|
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 javax.microedition.m2g.ExternalResourceHandler; |
|
22 import com.nokia.microedition.m2g.connection.common.M2GConnectionPolicyImpl; |
|
23 |
|
24 /** |
|
25 * Connection factory |
|
26 */ |
|
27 public class M2GConnectionFactory |
|
28 { |
|
29 //-------------------------------------------------- |
|
30 // STATIC CONSTANTS |
|
31 //-------------------------------------------------- |
|
32 public static final String COMMON_PROTOCOL = |
|
33 "common"; |
|
34 public static final String FILE_PROTOCOL = |
|
35 "file"; |
|
36 public static final String CONNECTION_BASE_PACKAGE = |
|
37 "com.nokia.microedition.m2g.connection."; |
|
38 public static final String CONNECTION_PROXY_CLASS_NAME = |
|
39 ".M2GConnectionProxyImpl"; |
|
40 |
|
41 |
|
42 //-------------------------------------------------- |
|
43 // METHODS |
|
44 //-------------------------------------------------- |
|
45 /** |
|
46 * Ctor |
|
47 */ |
|
48 private M2GConnectionFactory() |
|
49 { |
|
50 } |
|
51 |
|
52 /** |
|
53 * Create a connection proxy. |
|
54 * If a url is not absolute or if it's beginig with |
|
55 * the slash mark then a resource is considered to be opened from a jar file. |
|
56 * @param aUrl Locator |
|
57 * @return Connection proxy |
|
58 * @throws IOException if an io error occurs |
|
59 * @throws NullPointerException if the locator is null. |
|
60 * @throws IllegalArgumentException if the locator is of unsupported type. |
|
61 */ |
|
62 public static M2GConnectionProxy create(String aUrl) throws IOException |
|
63 { |
|
64 try |
|
65 { |
|
66 M2GConnectionProxy proxy = null; |
|
67 M2GUrlTokenizer tokenizer = new M2GUrlTokenizer(aUrl); |
|
68 |
|
69 // If a url begins with the slash mark then |
|
70 // a resource is tryed to open from a jar file. |
|
71 if (!M2GUrlTokenizer.isAbsolutURL(aUrl) || |
|
72 tokenizer.beginWithSlash()) |
|
73 { |
|
74 proxy = new M2GConnectionProxy(); |
|
75 } |
|
76 else |
|
77 { |
|
78 Class proxyClass = |
|
79 Class.forName(parseClassName(tokenizer.getProtocol())); |
|
80 proxy = (M2GConnectionProxy)proxyClass.newInstance(); |
|
81 } |
|
82 proxy.open(tokenizer); |
|
83 return proxy; |
|
84 } |
|
85 catch (InstantiationException e) |
|
86 { |
|
87 throw new IllegalArgumentException("Internal error: " + e.getMessage()); |
|
88 } |
|
89 catch (IllegalAccessException e) |
|
90 { |
|
91 throw new IllegalArgumentException("Internal error: " + e.getMessage()); |
|
92 } |
|
93 catch (ClassNotFoundException e) |
|
94 { |
|
95 throw new IllegalArgumentException("Internal error: " + e.getMessage()); |
|
96 } |
|
97 } |
|
98 |
|
99 /** |
|
100 * Get default external resource handler |
|
101 * @param aUrl URL |
|
102 * @return external resource handler |
|
103 */ |
|
104 public static ExternalResourceHandler getExternalResourceHandler(String aUrl) |
|
105 { |
|
106 return new M2GDefaultExternalResourceHandler(aUrl); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Resolve external resource URL |
|
111 * @param aTokenizer Tokenizer |
|
112 * @param aUrl URL |
|
113 * @return connection proxy |
|
114 * @throws IOException |
|
115 */ |
|
116 public static M2GConnectionProxy resolveExternalResourceUrl( |
|
117 M2GUrlTokenizer aTokenizer, String aUrl) throws IOException |
|
118 { |
|
119 // Check if the URL is relative. |
|
120 if (!M2GUrlTokenizer.isAbsolutURL(aUrl)) |
|
121 { |
|
122 if (aTokenizer.getBaseUrl() != null) |
|
123 { |
|
124 aUrl = aTokenizer.resolveUrl(aUrl); |
|
125 } |
|
126 } |
|
127 |
|
128 return M2GConnectionFactory.create(aUrl); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Get default policy |
|
133 * @return Connection policy |
|
134 */ |
|
135 public static M2GConnectionPolicy getDefaultPolicy() |
|
136 { |
|
137 return new M2GConnectionPolicyImpl(); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Parses a connection proxy class name. |
|
142 * @param aProtocol Protocol |
|
143 * @return Connection proxy class name |
|
144 */ |
|
145 private static String parseClassName(String aProtocol) |
|
146 { |
|
147 if (aProtocol.equals(FILE_PROTOCOL)) |
|
148 { |
|
149 return |
|
150 CONNECTION_BASE_PACKAGE + FILE_PROTOCOL + CONNECTION_PROXY_CLASS_NAME; |
|
151 } |
|
152 else |
|
153 { |
|
154 return |
|
155 CONNECTION_BASE_PACKAGE + COMMON_PROTOCOL + CONNECTION_PROXY_CLASS_NAME; |
|
156 |
|
157 } |
|
158 } |
|
159 } |