KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
25
26 import org.jboss.deployers.spi.DeploymentException;
27 import org.jboss.deployers.spi.deployer.DeploymentUnit;
28 import org.jboss.virtual.VirtualFile;
29
30 /**
31  * AbstractParsingDeployer. Extends AbstractTypedDeployer to add a notion of obtaining an instance of the
32  * deploymentType by parsing a metadata file. Subclasses need to override
33  * parse(DeploymentUnit, VirtualFile) to define this behavior.
34  *
35  * @param <T> the expected type
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 1.1 $
38  */

39 public abstract class AbstractParsingDeployer<T> extends AbstractTypedDeployer<T>
40 {
41    /**
42     * Create a new AbstractParsingDeployer. Sets the default relative order
43     * to PARSER_DEPLOYER.
44     *
45     * @param deploymentType the deployment type
46     * @throws IllegalArgumentException if the deployment type is null
47     */

48    protected AbstractParsingDeployer(Class JavaDoc<T> deploymentType)
49    {
50       super(deploymentType);
51       // Default the relative order to PARSER_DEPLOYER
52
setRelativeOrder(PARSER_DEPLOYER);
53    }
54
55    /**
56     * A flag indicating whether createMetaData should execute a parse even if a non-null metadata value exists.
57     *
58     * @return false if a parse should be performed only if there is no existing metadata value. True indicates
59     * that parse should be done regardless of an existing metadata value.
60     */

61    protected boolean allowsReparse()
62    {
63       return false;
64    }
65
66    /**
67     * Get some meta data
68     *
69     * @param unit the deployment unit
70     * @param key the key into the managed objects
71     * @return the metadata or null if it doesn't exist
72     */

73    protected T getMetaData(DeploymentUnit unit, String JavaDoc key)
74    {
75       return unit.getAttachment(key, getDeploymentType());
76    }
77    
78    /**
79     * Create some meta data. Calls createMetaData(unit, name, suffix, getDeploymentType().getName()).
80     *
81     * @param unit the deployment unit
82     * @param name the name
83     * @param suffix the suffix
84     * @throws DeploymentException for any error
85     */

86    protected void createMetaData(DeploymentUnit unit, String JavaDoc name, String JavaDoc suffix) throws DeploymentException
87    {
88       createMetaData(unit, name, suffix, getDeploymentType().getName());
89    }
90    
91    /**
92     * Create some meta data. Invokes parse(unit, name, suffix) if there is not already a
93     * metadata
94     *
95     * @param unit the deployment unit
96     * @param name the name
97     * @param suffix the suffix
98     * @param key the key into the managed objects
99     * @throws DeploymentException for any error
100     */

101    protected void createMetaData(DeploymentUnit unit, String JavaDoc name, String JavaDoc suffix, String JavaDoc key) throws DeploymentException
102    {
103       // First see whether it already exists
104
T result = getMetaData(unit, key);
105       if (result != null && allowsReparse() == false)
106          return;
107
108       // Create it
109
try
110       {
111          if (suffix == null)
112             result = parse(unit, name, result);
113          else
114             result = parse(unit, name, suffix, result);
115       }
116       catch (Exception JavaDoc e)
117       {
118          throw DeploymentException.rethrowAsDeploymentException("Error creating managed object for " + unit.getName(), e);
119       }
120       
121       // Doesn't exist
122
if (result == null)
123          return;
124       
125       // Register it
126
unit.getTransientManagedObjects().addAttachment(key, result, getDeploymentType());
127    }
128
129    /**
130     * Parse an exact file name
131     *
132     * @param unit the unit
133     * @param name the exact name to match
134     * @param root - possibly null pre-existing root
135     * @return the metadata or null if it doesn't exist
136     * @throws Exception for any error
137     */

138    protected T parse(DeploymentUnit unit, String JavaDoc name, T root) throws Exception JavaDoc
139    {
140       // Try to find the metadata
141
VirtualFile file = unit.getMetaDataFile(name);
142       if (file == null)
143          return null;
144       
145       T result = parse(unit, file, root);
146       init(unit, result, file);
147       return result;
148    }
149    
150    /**
151     * Parse an exact file name
152     *
153     * @param unit the unit
154     * @param name the exact name to match
155     * @param suffix the suffix to match
156     * @param root - possibly null pre-existing root
157     * @return the metadata or null if it doesn't exist
158     * @throws Exception for any error
159     */

160    protected T parse(DeploymentUnit unit, String JavaDoc name, String JavaDoc suffix, T root) throws Exception JavaDoc
161    {
162       // Try to find the metadata
163
List JavaDoc<VirtualFile> files = unit.getMetaDataFiles(name, suffix);
164       if (files.size() == 0)
165          return null;
166       
167       // TODO remove this limitation
168
if (files.size() > 1)
169          throw new DeploymentException("Only one file is allowed, found=" + files);
170
171       VirtualFile file = files.get(0);
172       
173       T result = parse(unit, file, root);
174       init(unit, result, file);
175       return result;
176    }
177    
178    /**
179     * Parse a deployment
180     *
181     * @param unit the deployment unit
182     * @param file the metadata file
183     * @param root - possibly null pre-existing root
184     * @return the metadata
185     * @throws Exception for any error
186     */

187    protected abstract T parse(DeploymentUnit unit, VirtualFile file, T root) throws Exception JavaDoc;
188    
189    /**
190     * Initialise the metadata
191     *
192     * @param unit the unit
193     * @param metaData the metadata
194     * @param file the metadata file
195     * @throws Exception for any error
196     */

197    protected void init(DeploymentUnit unit, T metaData, VirtualFile file) throws Exception JavaDoc
198    {
199    }
200 }
Popular Tags