001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.deployers;
023:
024: import org.jboss.deployers.plugins.deployers.helpers.AbstractTypedDeployer;
025: import org.jboss.deployers.spi.DeploymentException;
026: import org.jboss.deployers.spi.deployer.DeploymentUnit;
027: import org.jboss.ejb3.metamodel.ApplicationClientDD;
028: import org.jboss.virtual.VirtualFile;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.lang.reflect.Field;
033: import java.util.jar.Attributes;
034: import java.util.jar.Manifest;
035:
036: /**
037: * Scan the main & super classes for annotations.
038: *
039: * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
040: * @version $Revision: $
041: */
042: public class AppClientScanningDeployer extends
043: AbstractTypedDeployer<ApplicationClientDD> {
044:
045: public AppClientScanningDeployer() {
046: super (ApplicationClientDD.class);
047:
048: setRelativeOrder(POSTPROCESS_CLASSLOADING_DEPLOYER);
049: }
050:
051: /* (non-Javadoc)
052: * @see org.jboss.deployers.plugins.deployer.AbstractSimpleDeployer#deploy(org.jboss.deployers.spi.deployer.DeploymentUnit)
053: */
054: @Override
055: public void deploy(DeploymentUnit unit) throws DeploymentException {
056: ApplicationClientDD dd = unit
057: .getAttachment(getDeploymentType());
058: // FIXME: implement metadata complete
059: // if(dd != null && dd.getMetaDataComplete())
060: // return;
061: // for now the EJB3 client deployer handles all
062: if (dd != null)
063: return;
064:
065: try {
066: String mainClassName = getMainClassName(unit);
067: if (mainClassName == null)
068: return;
069:
070: Class<?> mainClass = unit.getClassLoader().loadClass(
071: mainClassName);
072:
073: log.info("mainClass = " + mainClass);
074:
075: if (hasAnnotations(mainClass)) {
076: // add a dummy application client dd to fire up the ejb3 client deployer
077: dd = new ApplicationClientDD();
078: unit.addAttachment(ApplicationClientDD.class, dd);
079: }
080: } catch (ClassNotFoundException e) {
081: throw new DeploymentException(e);
082: } catch (IOException e) {
083: throw new DeploymentException(e);
084: }
085: }
086:
087: // TODO: integrate with Ejb3ClientDeployer.getMainClassName
088: private String getMainClassName(DeploymentUnit unit)
089: throws IOException {
090: VirtualFile file = unit.getMetaDataFile("MANIFEST.MF");
091: log.trace("parsing " + file);
092:
093: if (file == null) {
094: return null;
095: }
096:
097: try {
098: // TODO - use VFSUtils.readManifest
099: InputStream is = file.openStream();
100: Manifest mf;
101: try {
102: mf = new Manifest(is);
103: } finally {
104: is.close();
105: }
106: Attributes attrs = mf.getMainAttributes();
107: String className = attrs
108: .getValue(Attributes.Name.MAIN_CLASS);
109: return className;
110: } finally {
111: file.close();
112: }
113: }
114:
115: // TODO: should we check for type of annotations?
116: private boolean hasAnnotations(Class<?> cls) {
117: if (cls == null)
118: return false;
119:
120: // Note: this also returns true if super class has annotations
121: if (cls.getAnnotations().length > 0)
122: return true;
123:
124: for (Field f : cls.getDeclaredFields()) {
125: if (f.getAnnotations().length > 0)
126: return true;
127: }
128:
129: return hasAnnotations(cls.getSuperclass());
130: }
131: }
|