KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > types > AbstractJavaType


1 /*
2  */

3 package com.sslexplorer.extensions.types;
4
5 import java.io.IOException JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.jdom.Element;
11
12 import com.sslexplorer.extensions.ExtensionDescriptor;
13 import com.sslexplorer.extensions.ExtensionException;
14 import com.sslexplorer.extensions.ExtensionType;
15 import com.sslexplorer.security.SessionInfo;
16
17 /**
18  * Abstract implementation of an {@link ExtensionType} that allows execution of
19  * Java applications.
20  *
21  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
22  */

23 public abstract class AbstractJavaType implements ExtensionType {
24
25     static Log log = LogFactory.getLog(AbstractJavaType.class);
26
27     // Private instance variables
28
private String JavaDoc jre;
29     private ExtensionDescriptor descriptor;
30     private String JavaDoc typeName;
31     private boolean canStop;
32
33     /**
34      * Constructor.
35      *
36      * @param typeName type name
37      */

38     public AbstractJavaType(String JavaDoc typeName, boolean canStop) {
39         this.typeName = typeName;
40         this.canStop = canStop;
41     }
42
43     /*
44      * (non-Javadoc)
45      *
46      * @see com.sslexplorer.extensions.ExtensionType#start(com.sslexplorer.extensions.ExtensionDescriptor,
47      * org.jdom.Element)
48      */

49     public void start(ExtensionDescriptor descriptor, Element element) throws ExtensionException {
50         this.descriptor = descriptor;
51         if (element.getName().equals(typeName)) {
52
53             jre = element.getAttribute("jre").getValue();
54
55             if (jre == null) {
56                 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
57                     "<application> element requires attribute 'jre'");
58             }
59
60             try {
61                 ExtensionDescriptor.getVersion(jre);
62             } catch (Throwable JavaDoc ex) {
63                 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, "Invalid value '" + jre
64                     + "' specified for 'jre' attribute");
65             }
66
67             for (Iterator JavaDoc it = element.getChildren().iterator(); it.hasNext();) {
68                 Element e = (Element) it.next();
69
70                 if (e.getName().equalsIgnoreCase("classpath")) {
71                     verifyClasspath(e);
72                 } else if (e.getName().equalsIgnoreCase("main")) {
73                     verifyMain(e);
74                 } else {
75                     throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, "Unexpected element <"
76                         + e.getName() + "> found in <application>");
77                 }
78             }
79
80         }
81
82     }
83
84     public void verifyRequiredElements() throws ExtensionException {
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see com.sslexplorer.extensions.ExtensionType#isHidden()
91      */

92     public boolean isHidden() {
93         return false;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see com.sslexplorer.extensions.ExtensionType#getType()
100      */

101     public String JavaDoc getType() {
102         return typeName;
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see com.sslexplorer.extensions.ExtensionType#stop()
109      */

110     public void stop() throws ExtensionException {
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see com.sslexplorer.extensions.ExtensionType#canStop()
117      */

118     public boolean canStop() {
119         return canStop;
120     }
121     
122     /* (non-Javadoc)
123      * @see com.sslexplorer.extensions.ExtensionType#active()
124      */

125     public void activate() throws ExtensionException {
126     }
127
128     private void verifyClasspath(Element element) throws ExtensionException {
129         for (Iterator JavaDoc it = element.getChildren().iterator(); it.hasNext();) {
130             Element e = (Element) it.next();
131             if (e.getName().equalsIgnoreCase("jar")) {
132                 descriptor.processFile(e);
133             } else if (e.getName().equals("if")) {
134                 verifyClasspath(e);
135             } else {
136                 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, "Invalid element <" + e.getName()
137                     + "> found in <classpath>");
138             }
139         }
140     }
141
142     private void verifyMain(Element element) throws ExtensionException {
143         for (Iterator JavaDoc it = element.getChildren().iterator(); it.hasNext();) {
144             Element e = (Element) it.next();
145             if (e.getName().equalsIgnoreCase("if")) {
146                 verifyMain(e);
147             } else if (!e.getName().equalsIgnoreCase("env") && !e.getName().equalsIgnoreCase("arg") && !e.getName().equalsIgnoreCase("jvm")) {
148                 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, "Unexpected element <" + e.getName()
149                     + "> found in <main>");
150             }
151         }
152     }
153
154     /* (non-Javadoc)
155      * @see com.sslexplorer.extensions.ExtensionType#descriptorCreated(org.jdom.Element)
156      */

157     public void descriptorCreated(Element element, SessionInfo session) throws IOException JavaDoc {
158     }
159 }
Popular Tags