View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.component.factory.desc;
17  
18  import java.io.InputStream;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.xml.XMLConstants;
24  import javax.xml.parsers.SAXParser;
25  import javax.xml.parsers.SAXParserFactory;
26  import javax.xml.transform.stream.StreamSource;
27  import javax.xml.validation.Schema;
28  import javax.xml.validation.SchemaFactory;
29  
30  import org.seasar.framework.container.factory.ClassPathResourceResolver;
31  import org.seasar.framework.exception.ResourceNotFoundRuntimeException;
32  import org.seasar.framework.exception.SAXRuntimeException;
33  import org.seasar.framework.util.InputStreamUtil;
34  import org.seasar.framework.util.OgnlUtil;
35  import org.seasar.framework.util.SAXParserFactoryUtil;
36  import org.seasar.framework.xml.SaxHandler;
37  import org.seasar.framework.xml.SaxHandlerParser;
38  import org.seasar.uruma.component.factory.desc.handler.UcdTagHandlerRule;
39  import org.seasar.uruma.core.UrumaConstants;
40  import org.seasar.uruma.util.AssertionUtil;
41  import org.seasar.uruma.util.resource.ResourceHandler;
42  import org.seasar.uruma.util.resource.ResourceTraverser;
43  import org.seasar.uruma.util.resource.ResourceTraverserFactory;
44  import org.seasar.uruma.util.resource.impl.ExtResourceFilter;
45  import org.xml.sax.SAXException;
46  
47  /**
48   * @author y-komori
49   */
50  public class ComponentRegistry implements ResourceHandler {
51      protected ClassPathResourceResolver resolver = new ClassPathResourceResolver();
52  
53      private List<UrumaComponentDesc> descs = new ArrayList<UrumaComponentDesc>();
54  
55      /**
56       * 
57       */
58      public void registComponents() {
59          registComponents(Thread.currentThread().getContextClassLoader());
60      }
61  
62      /**
63       * @param loader
64       */
65      public void registComponents(final ClassLoader loader) {
66          URL root = loader.getResource("");
67          URL origin = loader.getResource("components");
68          ResourceTraverser traverser = ResourceTraverserFactory
69                  .getResourceTraverser(origin);
70          ExtResourceFilter filter = new ExtResourceFilter("ucd");
71          traverser.traverse(root, origin, this, filter);
72      }
73  
74      /*
75       * @see
76       * org.seasar.uruma.util.resource.ResourceHandler#handle(java.lang.String,
77       * java.lang.String, java.io.InputStream)
78       */
79      public void handle(final String rootPath, final String path,
80              final InputStream is) {
81          System.err.println(path + " から読み込みます");
82          UrumaComponentDesc desc = load(path);
83          registComponent(desc);
84          System.err.println(desc);
85      }
86  
87      /**
88       * コンポーネント・ディスクリプタを登録します。<br />
89       * 
90       * @param desc
91       *            {@link UrumaComponentDesc} オブジェクト
92       */
93      public void registComponent(final UrumaComponentDesc desc) {
94          AssertionUtil.assertNotNull("desc", desc);
95          descs.add(desc);
96      }
97  
98      /**
99       * 指定されたパスから Uruma コンポーネントディスクリプタを読み込みます。<br />
100      * 
101      * @param path
102      *            コンポーネントディスクリプタファイルのパス
103      * @return {@link UrumaComponentDesc} オブジェクト
104      */
105     public UrumaComponentDesc load(final String path) {
106         final SaxHandlerParser parser = createSaxHandlerParser(path);
107         final InputStream is = getInputStream(path);
108         try {
109             UrumaComponentDesc desc = (UrumaComponentDesc) parser.parse(is,
110                     path);
111             return desc;
112         } finally {
113             InputStreamUtil.close(is);
114         }
115     }
116 
117     protected SaxHandlerParser createSaxHandlerParser(final String path) {
118         System.setProperty("javax.xml.parsers.SAXParserFactory",
119                 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
120         final SAXParserFactory factory = SAXParserFactoryUtil.newInstance();
121         factory.setNamespaceAware(true);
122 
123         final SchemaFactory schemaFactory = SchemaFactory
124                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
125         InputStream is = getInputStream(UrumaConstants.COMPONENT_DESC_SCHEMA_PATH);
126         try {
127             final Schema schema = schemaFactory.newSchema(new StreamSource(is));
128             factory.setSchema(schema);
129         } catch (SAXException ex) {
130             throw new SAXRuntimeException(ex);
131         } finally {
132             InputStreamUtil.close(is);
133         }
134 
135         final SAXParser saxParser = SAXParserFactoryUtil.newSAXParser(factory);
136         final SaxHandler handler = createSaxHandler();
137 
138         return new SaxHandlerParser(handler, saxParser);
139     }
140 
141     protected InputStream getInputStream(final String path) {
142         final InputStream is = resolver.getInputStream(path);
143 
144         if (is == null) {
145             throw new ResourceNotFoundRuntimeException(path);
146         }
147         return is;
148     }
149 
150     protected SaxHandler createSaxHandler() {
151         SaxHandler handler = new SaxHandler(new UcdTagHandlerRule());
152         return handler;
153     }
154 
155     protected void setupComponents() {
156 
157     }
158 
159     protected Object[] evaluateArgs(final List<String> argExprs) {
160         int size = argExprs.size();
161         Object[] args = new Object[size];
162         for (int i = 0; i < size; i++) {
163             Object exp = OgnlUtil.parseExpression(argExprs.get(i));
164             args[i] = OgnlUtil.getValue(exp, null);
165         }
166         return args;
167     }
168 }