View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Resolves the URI to a configuration
31   */
32  public abstract class UriResolver
33  {
34      /**
35       * The configuration
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           * metadata for the given entity
50           */
51          public Extractor getExtractor()
52          {
53              return extractor;
54          }
55  
56          /**
57           * the entity identification
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       * resolve the given uri
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       * Look in all META-INF directories in the classpath for a file of name
90       * "dynaForm-scheme.xml", where scheme is a parameter to this method.
91       * <p>
92       * From that file, load a Properties object and get the value of key
93       * "Extractor", which is expected to be the name of a class that implements
94       * the {@link org.apache.myfaces.orchestra.dynaForm.metadata.Extractor}
95       * interface.
96       * <p>
97       * Use the Extractor instance to return a simple (extractor, classname)
98       * pairing.
99  TODO: use the normal java Service configuration approach instead.
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                     // do not shadow the real exception
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      * load the configuration
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 }