View Javadoc

1   /*
2    *
3    * The Seasar Software License, Version 1.1
4    *
5    * Copyright (c) 2003-2004 The Seasar Project. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   *
11   * 1. Redistributions of source code must retain the above
12   *    copyrightnotice, this list of conditions and the following
13   *    disclaimer.
14   *
15   * 2. Redistributions in binary form must reproduce the above
16   *    copyright notice, this list of conditions and the following
17   *    disclaimer in the documentation and/or other materials provided
18   *    with the distribution.
19   *
20   * 3. The end-user documentation included with the redistribution,
21   *    if any, must include the following acknowledgement:
22   *    "This product includes software developed by the
23   *    Seasar Project (http://www.seasar.org/)."
24   *    Alternately, this acknowledgement may appear in the software
25   *    itself, if and wherever such third-party acknowledgements
26   *    normally appear.
27   *
28   * 4. Neither the names "The Seasar Project" nor the name of its
29   *    contributors ay be used to endour or promote products derived
30   *    from this software without specific prior written permission of
31   *    the Seasar Project.
32   *
33   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR
34   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35   * WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE SEASER PROJECT
37   * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38   * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42   * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
43   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.seasar.groovy;
48  
49  import groovy.lang.GroovyShell;
50  
51  import org.codehaus.groovy.control.CompilationFailedException;
52  
53  import org.seasar.framework.container.S2Container;
54  import org.seasar.framework.container.factory.S2ContainerBuilder;
55  import org.seasar.framework.util.ResourceUtil;
56  
57  import java.io.IOException;
58  import java.io.InputStream;
59  
60  
61  /***
62   * Groovyスクリプトを読みこんでS2Containerを作成するファクトリークラス。
63   * 読みこまれるGroovyスクリプトは、スクリプト内でS2Containerを作成し、 returnする必要がある。
64   *
65   * @author takai
66   */
67  public class S2ContainerGroovyFactory
68      implements S2ContainerBuilder
69  {
70      protected S2ContainerGroovyFactory() {
71      }
72  
73      /***
74       * GroovyスクリプトからS2Containerを作成する。
75       *
76       * @param path 読みこむGroovyスクリプトのパス
77       *
78       * @return Groovyスクリプトによって作成されたS2Container
79       *
80       * @throws SeasarBuilderException Groovyスクリプトにエラーがあった場合に発生する
81       */
82      public static S2Container create(String path)
83          throws SeasarBuilderException
84      {
85          return new S2ContainerGroovyFactory().build(path);
86      }
87  
88      /***
89       * @see org.seasar.framework.container.factory.S2ContainerBuilder#build(java.lang.String)
90       */
91      public S2Container build(String path)
92          throws SeasarBuilderException
93      {
94          S2Container container = null;
95  
96          try {
97              InputStream stream = ResourceUtil.getResourceAsStream(path);
98              GroovyShell shell = new GroovyShell();
99              container = (S2Container) shell.evaluate(stream, path);
100 
101             if (container == null) {
102                 throw new SeasarBuilderException("The return value is null or not returning.");
103             }
104 
105             if (!(container instanceof S2Container)) {
106                 throw new SeasarBuilderException("The return value is not instance of S2Container");
107             }
108         } catch (CompilationFailedException e) {
109             throw new SeasarBuilderException(e);
110         } catch (IOException e) {
111             throw new SeasarBuilderException(e);
112         } catch (RuntimeException e) {
113             throw new SeasarBuilderException(e);
114         } catch (NoClassDefFoundError e) {
115             throw new SeasarBuilderException(e);
116         }
117 
118         return container;
119     }
120 
121     /***
122      * @see org.seasar.framework.container.factory.S2ContainerBuilder#build(java.lang.String,
123      *      java.lang.ClassLoader)
124      */
125     public S2Container build(String path, ClassLoader classLoader)
126         throws SeasarBuilderException
127     {
128         S2Container container = null;
129         ClassLoader old = Thread.currentThread().getContextClassLoader();
130 
131         try {
132             if (classLoader != null) {
133                 Thread.currentThread().setContextClassLoader(classLoader);
134             }
135 
136             container = build(path);
137         } finally {
138             Thread.currentThread().setContextClassLoader(old);
139         }
140 
141         return container;
142     }
143 
144 
145     /*
146      * NOTE 仕様はこれで正しい?
147 	 * @see org.seasar.framework.container.factory.S2ContainerBuilder#include(org.seasar.framework.container.S2Container, java.lang.String)
148 	 */
149 	public S2Container include(S2Container parent, String path) {
150 		S2Container container = build(path);
151 		parent.include(container);
152 		
153 		return parent;
154 	}
155 }