KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > deployers > helpers > ObjectModelFactoryDeployer


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.deployers.plugins.deployers.helpers;
23
24 import java.net.URL JavaDoc;
25
26 import org.jboss.deployers.spi.DeploymentException;
27 import org.jboss.deployers.spi.deployer.DeploymentUnit;
28 import org.jboss.virtual.VirtualFile;
29 import org.jboss.xb.binding.ObjectModelFactory;
30 import org.jboss.xb.binding.Unmarshaller;
31 import org.jboss.xb.binding.UnmarshallerFactory;
32
33 /**
34  * ObjectModelFactoryDeployer extends the AbstractParsingDeployer to add an
35  * abstract JBossXB ObjectModelFactory accessor that is used from within an
36  * overriden parse(DeploymentUnit unit, VirtualFile file) to unmarshall the xml
37  * document represented by file into an instance of deploymentType T.
38  *
39  * @param <T> the expected type
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 1.1 $
43  */

44 public abstract class ObjectModelFactoryDeployer<T> extends AbstractParsingDeployer<T>
45 {
46    /** Unmarshaller factory */
47    private static final UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
48    
49    /**
50     * Create a new SchemaResolverDeployer.
51     *
52     * @param deploymentType the deployment type
53     * @throws IllegalArgumentException for a null deployment type
54     */

55    public ObjectModelFactoryDeployer(Class JavaDoc<T> deploymentType)
56    {
57       super(deploymentType);
58    }
59
60    /**
61     * Parse a deployment. The entails obtaining the metadata file contents via
62     * its URL handler, creating a jbossxb Unmarshaller, and unmarshalling the
63     * metdata into deployment type T via the associated ObjectModelFactory.
64     *
65     * @param unit the deployment unit
66     * @param file the metadata file
67     * @param root - possibly null pre-existing root. It should be null if there
68     * is no pre-existing that should be passed to the ObjectModelFactory.
69     * @return the metadata
70     * @throws Exception for any error
71     */

72    protected T parse(DeploymentUnit unit, VirtualFile file, T root) throws Exception JavaDoc
73    {
74       if (file == null)
75          throw new IllegalArgumentException JavaDoc("Null file");
76
77       Unmarshaller unmarshaller = factory.newUnmarshaller();
78       Object JavaDoc parsed = null;
79       try
80       {
81          ObjectModelFactory factory = getObjectModelFactory(root);
82          URL JavaDoc url = file.toURL();
83          parsed = unmarshaller.unmarshal(url.toString(), factory, root);
84       }
85       catch (Throwable JavaDoc t)
86       {
87          DeploymentException.rethrowAsDeploymentException("Error parsing meta data " + file.getPathName(), t);
88       }
89       if (parsed == null)
90          throw new DeploymentException("The xml " + file.getPathName() + " is not well formed!");
91
92       return getDeploymentType().cast(parsed);
93    }
94
95    /**
96     * Get the object model factory
97     *
98     * @param root - possibly null pre-existing root
99     * @return the object model factory
100     */

101    protected abstract ObjectModelFactory getObjectModelFactory(T root);
102 }
103
Popular Tags