KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > deployment > xml > BasicXMLDeployer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.kernel.plugins.deployment.xml;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.jboss.kernel.Kernel;
30 import org.jboss.kernel.plugins.deployment.BasicKernelDeployer;
31 import org.jboss.kernel.spi.deployment.KernelDeployment;
32 import org.jboss.logging.Logger;
33 import org.jboss.util.collection.CollectionsFactory;
34 import org.jboss.xb.binding.sunday.unmarshalling.SingletonSchemaResolverFactory;
35 import org.jboss.xb.binding.Unmarshaller;
36 import org.jboss.xb.binding.UnmarshallerFactory;
37 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
38
39 /**
40  * BasicXMLDeployer.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 57133 $
44  */

45 public class BasicXMLDeployer extends BasicKernelDeployer
46 {
47    /** The log */
48    private static final Logger log = Logger.getLogger(BasicXMLDeployer.class);
49
50    /** Unmarshaller factory */
51    private static final UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
52
53    /** The resolver */
54    private static final SchemaBindingResolver resolver = SingletonSchemaResolverFactory.getInstance().getSchemaBindingResolver();
55
56    /** The deployments by url or name */
57    private Map JavaDoc<String JavaDoc, KernelDeployment> deploymentsByName = CollectionsFactory.createConcurrentReaderMap();
58
59    /**
60     * Create a new XML deployer
61     *
62     * @param kernel the kernel
63     */

64    public BasicXMLDeployer(Kernel kernel)
65    {
66       super(kernel);
67    }
68
69    public Collection JavaDoc getDeploymentNames()
70    {
71       return deploymentsByName.keySet();
72    }
73    
74    public void deploy(KernelDeployment deployment) throws Throwable JavaDoc
75    {
76       super.deploy(deployment);
77       deploymentsByName.put(deployment.getName(), deployment);
78    }
79
80    public void undeploy(KernelDeployment deployment)
81    {
82       deploymentsByName.remove(deployment.getName());
83       super.undeploy(deployment);
84    }
85
86    /**
87     * Undeploy a url
88     *
89     * @param url the url to undeploy
90     * @throws IllegalStateException if the url is unknown
91     */

92    public void undeploy(final URL JavaDoc url)
93    {
94       if (url == null)
95          throw new IllegalArgumentException JavaDoc("Null url");
96       undeploy(url.toString());
97    }
98
99    /**
100     * Undeploy a name deployment
101     *
102     * @param name the name of the deployment to undeploy
103     * @throws IllegalStateException if the name is unknown
104     */

105    public void undeploy(final String JavaDoc name)
106    {
107       if (name == null)
108          throw new IllegalArgumentException JavaDoc("Null name");
109       KernelDeployment deployment = deploymentsByName.remove(name);
110       if (deployment == null)
111          throw new IllegalStateException JavaDoc("Unknown deployment " + name);
112       undeploy(deployment);
113    }
114
115    /**
116     * Deploy a url
117     *
118     * @param url the url to deploy
119     * @return the kernel deployment
120     * @throws Throwable for any error
121     */

122    public KernelDeployment deploy(final URL JavaDoc url) throws Throwable JavaDoc
123    {
124       final boolean trace = log.isTraceEnabled();
125
126       if (url == null)
127          throw new IllegalArgumentException JavaDoc("Null url");
128
129       if (trace)
130          log.trace("Parsing " + url);
131
132       long start = System.currentTimeMillis();
133
134       Unmarshaller unmarshaller = factory.newUnmarshaller();
135       KernelDeployment deployment = (KernelDeployment) unmarshaller.unmarshal(url.toString(), resolver);
136       if (deployment == null)
137          throw new RuntimeException JavaDoc("The xml " + url + " is not well formed!");
138       deployment.setName(url.toString());
139
140       if (trace)
141       {
142          long now = System.currentTimeMillis();
143          log.trace("Parsing " + url + " took " + (now-start) + " milliseconds");
144       }
145
146       deploy(deployment);
147
148       if (trace)
149       {
150          long now = System.currentTimeMillis();
151          log.trace("Deploying " + url + " took " + (now-start) + " milliseconds");
152       }
153
154       return deployment;
155    }
156
157    /**
158     * Deploy a stream. We may be deploying XML fragments.
159     *
160     * @param deploymentName the deployment name
161     * @param stream the stream
162     * @return the kernel deployment
163     * @throws Throwable for any error
164     */

165    public KernelDeployment deploy(String JavaDoc deploymentName, final InputStream JavaDoc stream) throws Throwable JavaDoc
166    {
167       boolean trace = log.isTraceEnabled();
168
169       if (trace)
170          log.trace("Parsing " + deploymentName);
171       Unmarshaller unmarshaller = factory.newUnmarshaller();
172       KernelDeployment deployment = (KernelDeployment) unmarshaller.unmarshal(stream, resolver);
173       if (deployment == null)
174          throw new RuntimeException JavaDoc("The deployment " + deploymentName + " is not well formed!");
175       deployment.setName(deploymentName);
176
177       deploy(deployment);
178
179       return deployment;
180    }
181 }
182
Popular Tags