KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > spring > deployment > SPRConfigBuilder


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.spring.deployment;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.geronimo.common.DeploymentException;
33 import org.apache.geronimo.deployment.ConfigurationBuilder;
34 import org.apache.geronimo.gbean.GBeanData;
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.gbean.GBeanInfoBuilder;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.kernel.Kernel;
39 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
40 import org.apache.geronimo.kernel.config.ConfigurationData;
41 import org.apache.geronimo.kernel.repository.Repository;
42 import org.apache.geronimo.spring.SpringApplicationImpl;
43 import org.apache.geronimo.spring.SpringGBean;
44
45 /**
46  * @version $Rev: 126313 $ $Date: 2005-01-24 21:03:52 +0000 (Mon, 24 Jan 2005) $
47  */

48 public class SPRConfigBuilder
49   implements ConfigurationBuilder
50 {
51   protected static final Log _log=LogFactory.getLog(SPRConfigBuilder.class);
52   protected static final String JavaDoc _defaultConfigPath="META-INF/spring.xml";
53
54   protected final Kernel _kernel;
55   protected final Repository _repository;
56   protected final URI JavaDoc _defaultParentId;
57
58   public
59     SPRConfigBuilder(URI JavaDoc defaultParentId, Repository repository, Kernel kernel)
60   {
61     _kernel =kernel;
62     _repository =repository;
63     _defaultParentId=defaultParentId;
64   }
65
66   //----------------------------------------
67
// RTTI
68

69   public static final GBeanInfo GBEAN_INFO;
70
71   static
72   {
73     GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(SPRConfigBuilder.class, NameFactory.CONFIG_BUILDER);
74     infoFactory.addAttribute("defaultParentId" , URI JavaDoc.class, true);
75     infoFactory.addReference("Repository" , Repository.class, NameFactory.GERONIMO_SERVICE);
76     infoFactory.addAttribute("kernel" , Kernel.class, false);
77     infoFactory.addInterface(ConfigurationBuilder.class);
78     infoFactory.setConstructor(new String JavaDoc[]{"defaultParentId", "Repository", "kernel"});
79
80     GBEAN_INFO = infoFactory.getBeanInfo();
81   }
82
83   public static GBeanInfo getGBeanInfo() {return GBEAN_INFO;}
84
85   //----------------------------------------
86

87   public Object JavaDoc
88     getDeploymentPlan(File JavaDoc planFile, JarFile JavaDoc sprFile)
89     throws DeploymentException
90   {
91     if (sprFile==null || !sprFile.getName().endsWith(".spr")) return null;
92
93     _log.info("Planning: "+sprFile.getName());
94
95     // N.B.
96
// - we should check that META-INF/spring.xml exists here (we can't really validate it)
97
// - we should check out META-INF/geronimo-spring.xml
98
// - could we inject stuff about environment into BeanFactory ?
99

100     return this; // token passed to buildConfiguration()...
101
}
102
103   public ConfigurationData
104     buildConfiguration(Object JavaDoc plan, JarFile JavaDoc sprFile, File JavaDoc outfile)
105     throws IOException JavaDoc, DeploymentException
106   {
107     if (!(plan instanceof SPRConfigBuilder)) // hacky...
108
return null;
109
110     String JavaDoc uid=sprFile.getName(); // should be overrideable in geronimo-spring.xml
111

112     _log.info("Building: "+uid);
113
114     SPRContext ctx=null;
115     try
116     {
117       URI JavaDoc configId=new URI JavaDoc(sprFile.getName()); // could be overridden in META-INF/geronimo-spring.xml
118
URI JavaDoc parentId=_defaultParentId; // could be overridden in META-INF/geronimo-spring.xml
119
URI JavaDoc configPath=new URI JavaDoc(_defaultConfigPath);
120
121       ctx=new SPRContext(outfile, configId, ConfigurationModuleType.SPR, parentId, _kernel);
122
123       // set up classpath and files that we want available in final
124
// distribution...
125
List JavaDoc classPath=new ArrayList JavaDoc();
126       classPath.add(new URI JavaDoc("."));
127
128       for (Enumeration JavaDoc e=sprFile.entries(); e.hasMoreElements();)
129       {
130         ZipEntry JavaDoc entry = (ZipEntry JavaDoc) e.nextElement();
131         String JavaDoc name=entry.getName();
132         ctx.addFile(URI.create(name), sprFile, entry);
133
134     if (name.endsWith(".jar"))
135       classPath.add(new URI JavaDoc(name));
136       }
137
138       // now we can get ClassLoader...
139
//ClassLoader cl=ctx.getClassLoader(_repository);
140

141       // managed Object for this Spring Application
142
{
143     ObjectName JavaDoc name=new ObjectName JavaDoc("geronimo.config", "name", uid);
144     GBeanData gbeanData=new GBeanData(name, SpringApplicationImpl.GBEAN_INFO);
145     ctx.addGBean(gbeanData);
146       }
147
148       // the actual Application...
149
{
150     Hashtable JavaDoc props=new Hashtable JavaDoc();
151         props.put("J2EEServer" , "geronimo");
152         props.put("J2EEApplication" , "null");
153         props.put("j2eeType" , "SpringModule");
154         props.put("name" , uid);
155     ObjectName JavaDoc on=new ObjectName JavaDoc("geronimo.server", props);
156     GBeanData gbeanData=new GBeanData(on, SpringGBean.GBEAN_INFO);
157         gbeanData.setAttribute("classPath" , classPath.toArray(new URI JavaDoc[classPath.size()]));
158         gbeanData.setAttribute("configPath" , configPath);
159     ctx.addGBean(gbeanData);
160       }
161     }
162     catch (Exception JavaDoc e)
163     {
164       throw new DeploymentException(e);
165     }
166     finally
167     {
168       if (ctx!=null) ctx.close();
169     }
170
171     return ctx.getConfigurationData();
172   }
173 }
174
Popular Tags