Coverage Report - org.seasar.uruma.component.factory.desc.ComponentRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentRegistry
51%
26/51
50%
2/4
0
 
 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  16
 public class ComponentRegistry implements ResourceHandler {
 51  16
     protected ClassPathResourceResolver resolver = new ClassPathResourceResolver();
 52  
 
 53  16
     private List<UrumaComponentDesc> descs = new ArrayList<UrumaComponentDesc>();
 54  
 
 55  
     /**
 56  
      * 
 57  
      */
 58  
     public void registComponents() {
 59  0
         registComponents(Thread.currentThread().getContextClassLoader());
 60  0
     }
 61  
 
 62  
     /**
 63  
      * @param loader
 64  
      */
 65  
     public void registComponents(final ClassLoader loader) {
 66  0
         URL root = loader.getResource("");
 67  0
         URL origin = loader.getResource("components");
 68  0
         ResourceTraverser traverser = ResourceTraverserFactory
 69  
                 .getResourceTraverser(origin);
 70  0
         ExtResourceFilter filter = new ExtResourceFilter("ucd");
 71  0
         traverser.traverse(root, origin, this, filter);
 72  0
     }
 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  0
         System.err.println(path + " から読み込みます");
 82  0
         UrumaComponentDesc desc = load(path);
 83  0
         registComponent(desc);
 84  0
         System.err.println(desc);
 85  0
     }
 86  
 
 87  
     /**
 88  
      * コンポーネント・ディスクリプタを登録します。<br />
 89  
      * 
 90  
      * @param desc
 91  
      *            {@link UrumaComponentDesc} オブジェクト
 92  
      */
 93  
     public void registComponent(final UrumaComponentDesc desc) {
 94  0
         AssertionUtil.assertNotNull("desc", desc);
 95  0
         descs.add(desc);
 96  0
     }
 97  
 
 98  
     /**
 99  
      * 指定されたパスから Uruma コンポーネントディスクリプタを読み込みます。<br />
 100  
      * 
 101  
      * @param path
 102  
      *            コンポーネントディスクリプタファイルのパス
 103  
      * @return {@link UrumaComponentDesc} オブジェクト
 104  
      */
 105  
     public UrumaComponentDesc load(final String path) {
 106  12
         final SaxHandlerParser parser = createSaxHandlerParser(path);
 107  12
         final InputStream is = getInputStream(path);
 108  
         try {
 109  8
             UrumaComponentDesc desc = (UrumaComponentDesc) parser.parse(is,
 110  
                     path);
 111  4
             return desc;
 112  
         } finally {
 113  4
             InputStreamUtil.close(is);
 114  
         }
 115  
     }
 116  
 
 117  
     protected SaxHandlerParser createSaxHandlerParser(final String path) {
 118  12
         System.setProperty("javax.xml.parsers.SAXParserFactory",
 119  
                 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
 120  12
         final SAXParserFactory factory = SAXParserFactoryUtil.newInstance();
 121  12
         factory.setNamespaceAware(true);
 122  
 
 123  12
         final SchemaFactory schemaFactory = SchemaFactory
 124  
                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 125  12
         InputStream is = getInputStream(UrumaConstants.COMPONENT_DESC_SCHEMA_PATH);
 126  
         try {
 127  12
             final Schema schema = schemaFactory.newSchema(new StreamSource(is));
 128  12
             factory.setSchema(schema);
 129  0
         } catch (SAXException ex) {
 130  0
             throw new SAXRuntimeException(ex);
 131  
         } finally {
 132  12
             InputStreamUtil.close(is);
 133  12
         }
 134  
 
 135  12
         final SAXParser saxParser = SAXParserFactoryUtil.newSAXParser(factory);
 136  12
         final SaxHandler handler = createSaxHandler();
 137  
 
 138  12
         return new SaxHandlerParser(handler, saxParser);
 139  
     }
 140  
 
 141  
     protected InputStream getInputStream(final String path) {
 142  24
         final InputStream is = resolver.getInputStream(path);
 143  
 
 144  24
         if (is == null) {
 145  4
             throw new ResourceNotFoundRuntimeException(path);
 146  
         }
 147  20
         return is;
 148  
     }
 149  
 
 150  
     protected SaxHandler createSaxHandler() {
 151  12
         SaxHandler handler = new SaxHandler(new UcdTagHandlerRule());
 152  12
         return handler;
 153  
     }
 154  
 
 155  
     protected void setupComponents() {
 156  
 
 157  0
     }
 158  
 
 159  
     protected Object[] evaluateArgs(final List<String> argExprs) {
 160  0
         int size = argExprs.size();
 161  0
         Object[] args = new Object[size];
 162  0
         for (int i = 0; i < size; i++) {
 163  0
             Object exp = OgnlUtil.parseExpression(argExprs.get(i));
 164  0
             args[i] = OgnlUtil.getValue(exp, null);
 165  
         }
 166  0
         return args;
 167  
     }
 168  
 }