1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.dynaForm.uri;
20
21 import org.apache.myfaces.orchestra.dynaForm.lib.DynaFormException;
22 import org.apache.myfaces.orchestra.dynaForm.metadata.Extractor;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.InvalidPropertiesFormatException;
27 import java.util.Properties;
28
29
30
31
32 public abstract class UriResolver
33 {
34
35
36
37 public static class Configuration
38 {
39 private final Extractor extractor;
40 private final String entity;
41
42 protected Configuration(Extractor extractor, String entity)
43 {
44 this.extractor = extractor;
45 this.entity = entity;
46 }
47
48
49
50
51 public Extractor getExtractor()
52 {
53 return extractor;
54 }
55
56
57
58
59 public String getEntity()
60 {
61 return entity;
62 }
63 }
64
65 protected Configuration createConfiguration(Extractor extractor, String entity)
66 {
67 return new Configuration(extractor, entity);
68 }
69
70
71
72
73 public Configuration resolveUri(String uri)
74 {
75 int pos = uri.indexOf(":");
76 if (pos < 0)
77 {
78 return resolve("default", uri);
79 }
80 if (uri.length() < pos + 1)
81 {
82 throw new IllegalArgumentException("Invalid uri: " + uri);
83 }
84
85 return resolve(uri.substring(0, pos), uri.substring(pos + 1));
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 protected Configuration resolve(String scheme, String path)
102 {
103 String config = "dynaForm-" + scheme + ".xml";
104 Properties props = new Properties();
105 InputStream resource = null;
106 try
107 {
108 resource = findConfig(config);
109 if (resource == null)
110 {
111 throw new DynaFormException("configuration '" + config + "' not found.");
112 }
113
114 props.loadFromXML(resource);
115 }
116 catch (InvalidPropertiesFormatException e)
117 {
118 throw new DynaFormException(e);
119 }
120 catch (IOException e)
121 {
122 throw new DynaFormException(e);
123 }
124 finally
125 {
126 if (resource != null)
127 {
128 try
129 {
130 resource.close();
131 }
132 catch (IOException e)
133 {
134
135 }
136 }
137 }
138
139 String extractor = getRequiredProperty(config, props, "Extractor");
140
141 try
142 {
143 Extractor extractorClass = (Extractor) Class.forName(extractor).newInstance();
144 return createConfiguration(
145 extractorClass,
146 path);
147 }
148 catch (InstantiationException e)
149 {
150 throw new DynaFormException(e);
151 }
152 catch (IllegalAccessException e)
153 {
154 throw new DynaFormException(e);
155 }
156 catch (ClassNotFoundException e)
157 {
158 throw new DynaFormException(e);
159 }
160 catch (SecurityException e)
161 {
162 throw new DynaFormException(e);
163 }
164 catch (IllegalArgumentException e)
165 {
166 throw new DynaFormException(e);
167 }
168 }
169
170
171
172
173 protected InputStream findConfig(String config)
174 {
175 return getResourceAsStream("META-INF/" + config);
176 }
177
178 protected InputStream getResourceAsStream(String resource)
179 {
180 InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
181 if (stream == null)
182 {
183 stream = UriResolver.class.getClassLoader().getResourceAsStream(resource);
184 }
185 return stream;
186 }
187
188 protected String getRequiredProperty(String config, Properties props, String key)
189 {
190 String value = props.getProperty(key);
191 if (value == null)
192 {
193 throw new IllegalStateException("Configuration '" + key + "' missing in config " + config);
194 }
195 return value;
196 }
197 }