|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.microedition.m2g.connection; |
|
19 |
|
20 import java.io.IOException; |
|
21 import java.io.InputStream; |
|
22 import javax.microedition.io.StreamConnection; |
|
23 import com.nokia.mj.impl.utils.Logger; |
|
24 |
|
25 |
|
26 /** |
|
27 * Base connection proxy class |
|
28 */ |
|
29 public class M2GConnectionProxy |
|
30 { |
|
31 //-------------------------------------------------- |
|
32 // STATIC CONSTANTS |
|
33 //-------------------------------------------------- |
|
34 // Error constants |
|
35 static final String RESOURCE_NOT_FOUND_ESTR = |
|
36 "Given resource is not found."; |
|
37 |
|
38 //-------------------------------------------------- |
|
39 // VARIABLES |
|
40 //-------------------------------------------------- |
|
41 protected String iBaseUrl = null; |
|
42 protected String iSuffixUrl = null; |
|
43 protected InputStream iInputStream = null; |
|
44 protected StreamConnection iConnection = null; |
|
45 protected M2GConnectionPolicy iConnectionPolicy = null; |
|
46 |
|
47 //-------------------------------------------------- |
|
48 // METHODS |
|
49 //-------------------------------------------------- |
|
50 |
|
51 /** |
|
52 * Ctor. |
|
53 */ |
|
54 public M2GConnectionProxy() |
|
55 { |
|
56 } |
|
57 |
|
58 |
|
59 /** |
|
60 * Close the connection. |
|
61 * When a connection has been closed, access to any of its |
|
62 * methods that involve an I/O operation will cause an |
|
63 * IOException to be thrown. |
|
64 * Closing an already closed connection has no effect. |
|
65 */ |
|
66 public void close() throws IOException |
|
67 { |
|
68 if (iInputStream != null) |
|
69 { |
|
70 iInputStream.close(); |
|
71 iInputStream = null; |
|
72 } |
|
73 if (iConnection != null) |
|
74 { |
|
75 iConnection.close(); |
|
76 iConnection = null; |
|
77 } |
|
78 iConnectionPolicy = null; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Get base url |
|
83 * @return Base url |
|
84 */ |
|
85 public String getBaseUrl() |
|
86 { |
|
87 return iBaseUrl; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Get the suffix url |
|
92 * @note The suffix is calculated in m2g.connection.file.M2GConnectionProxyImpl.open() |
|
93 * @return Suffix url including '?' if there is a suffix, and null otherwise |
|
94 */ |
|
95 public String getSuffixUrl() |
|
96 { |
|
97 return iSuffixUrl; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Get an input stream for a connection. |
|
102 * @return An input stream |
|
103 * @throws IOException If the input stream is null. |
|
104 */ |
|
105 public InputStream getInputStream() throws IOException |
|
106 { |
|
107 if (iInputStream == null) |
|
108 { |
|
109 throw new IOException(); |
|
110 } |
|
111 return iInputStream; |
|
112 } |
|
113 |
|
114 /** |
|
115 * Get a connection policy. |
|
116 * @return A connection policy |
|
117 * @throws IOException If the connection policy is null. |
|
118 */ |
|
119 public M2GConnectionPolicy getConnectionPolicy() throws IOException |
|
120 { |
|
121 if (iConnectionPolicy == null) |
|
122 { |
|
123 throw new IOException(); |
|
124 } |
|
125 return iConnectionPolicy; |
|
126 } |
|
127 |
|
128 |
|
129 /** |
|
130 * Open a connection to a file located in a jar file.. |
|
131 * @param aTokenizer Url tokenizer |
|
132 * @throws IOException If an error occurs while loading a content. |
|
133 * @throws IllegalArgumentException If no resource is found according to an URL. |
|
134 */ |
|
135 public void open(M2GUrlTokenizer aTokenizer) throws IOException |
|
136 { |
|
137 iBaseUrl = aTokenizer.getBaseUrl(); |
|
138 iSuffixUrl = null; // can't have DRM mode within a JAR |
|
139 |
|
140 iInputStream = |
|
141 Runtime.getRuntime().getClass().getResourceAsStream(aTokenizer.getUrl()); |
|
142 |
|
143 if (iInputStream == null) |
|
144 { |
|
145 Logger.ELOG(Logger.EJavaUI, RESOURCE_NOT_FOUND_ESTR); |
|
146 throw new IllegalArgumentException(RESOURCE_NOT_FOUND_ESTR); |
|
147 } |
|
148 iConnectionPolicy = M2GConnectionFactory.getDefaultPolicy(); |
|
149 } |
|
150 } |