KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sslexplorer.extensions.types;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jdom.Element;
12
13 import com.sslexplorer.boot.ContextHolder;
14 import com.sslexplorer.boot.Util;
15 import com.sslexplorer.extensions.ExtensionDescriptor;
16 import com.sslexplorer.extensions.ExtensionException;
17 import com.sslexplorer.extensions.ExtensionType;
18 import com.sslexplorer.language.Language;
19 import com.sslexplorer.language.LanguagePackDefinition;
20 import com.sslexplorer.language.LanguagePackManager;
21 import com.sslexplorer.security.SessionInfo;
22
23 /**
24  * Extension type that adds a new language pack.
25  *
26  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
27  */

28 public class LanguagePackType implements ExtensionType {
29
30     final static Log log = LogFactory.getLog(LanguagePackType.class);
31
32     /**
33      * Type name
34      */

35     public final static String JavaDoc TYPE = "languagePack";
36
37     // Private instance variables
38

39     private LanguagePackDefinition packDefinition;
40     private boolean classpathAdded;
41
42     public void start(ExtensionDescriptor descriptor, Element element) throws ExtensionException {
43
44         if (element.getName().equals(TYPE)) {
45
46             // Plugin name
47
String JavaDoc name = element.getAttributeValue("name");
48             if (name == null || name.equals("")) {
49                 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
50                     "The name attribute must be supplied for <languagePack> elements.");
51             }
52
53             // Create the definition
54
packDefinition = new LanguagePackDefinition(descriptor, name);
55             LanguagePackManager.getInstance().addLanguagePackDefinition(packDefinition);
56
57             for (Iterator JavaDoc i = element.getChildren().iterator(); i.hasNext();) {
58                 Element el = (Element) i.next();
59                 if (el.getName().equals("classpath")) {
60                     String JavaDoc path = Util.trimmedBothOrBlank(el.getText());
61                     if (path != null && !path.equals("")) {
62                         File JavaDoc f = new File JavaDoc(descriptor.getApplicationBundle().getBaseDir(), path);
63                         if (f.exists()) {
64                             try {
65                                 URL JavaDoc u = f.toURL();
66                                 if (log.isInfoEnabled())
67                                     log.info("Adding " + u + " to classpath");
68                                 packDefinition.addClassPath(u);
69                             } catch (MalformedURLException JavaDoc murle) {
70                             }
71                         } else {
72                             if (!"true".equals(System.getProperty("sslexplorer.useDevConfig"))) {
73                                 log.warn("Plugin classpath element " + f.getAbsolutePath() + " does not exist.");
74                             }
75                         }
76                     }
77                 } else if (el.getName().equals("date")) {
78                     String JavaDoc date = Util.trimmedBothOrBlank(el.getText());
79                     if (Util.isNullOrTrimmedBlank(date)) {
80                         throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
81                             "The content of the <date> must contain the creation date of the language in the pack.");
82                     }
83                     packDefinition.setDate(date);
84                 } else if (el.getName().equals("language")) {
85                     String JavaDoc code = el.getAttributeValue("code");
86                     if (code == null || code.equals("")) {
87                         throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
88                             "The code attribute must be supplied for <language> elements.");
89                     }
90                     String JavaDoc description = Util.trimmedBothOrBlank(el.getText());
91                     if (Util.isNullOrTrimmedBlank(description)) {
92                         throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
93                             "The content of the <language> must contain the description of the language in the pack.");
94                     }
95                     packDefinition.addLanguage(new Language(packDefinition, code, description));
96                 } else {
97                     throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR,
98                         "The <language> element only supports the nested <classpath> elements");
99                 }
100             }
101             
102             // Now add the classpath. We will only do this once until the dynamic classloader is written
103
if(!classpathAdded) {
104                 for (Iterator JavaDoc j = packDefinition.getClassPath().iterator(); j.hasNext();) {
105                     URL JavaDoc u = (URL JavaDoc) j.next();
106                     ContextHolder.getContext().addContextLoaderURL(u);
107                 }
108                 classpathAdded = true;
109             }
110         }
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see com.sslexplorer.extensions.ExtensionType#verifyRequiredElements()
117      */

118     public void verifyRequiredElements() throws ExtensionException {
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see com.sslexplorer.extensions.ExtensionType#isHidden()
125      */

126     public boolean isHidden() {
127         return true;
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see com.sslexplorer.extensions.ExtensionType#getType()
134      */

135     public String JavaDoc getType() {
136         return TYPE;
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see com.sslexplorer.extensions.ExtensionType#stop()
143      */

144     public void stop() throws ExtensionException {
145         if (packDefinition != null) {
146             LanguagePackManager.getInstance().removeLanguagePack(packDefinition);
147         }
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see com.sslexplorer.extensions.ExtensionType#active()
154      */

155     public void activate() throws ExtensionException {
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see com.sslexplorer.extensions.ExtensionType#canStop()
162      */

163     public boolean canStop() throws ExtensionException {
164         return true;
165     }
166
167     /* (non-Javadoc)
168      * @see com.sslexplorer.extensions.ExtensionType#descriptorCreated(org.jdom.Element)
169      */

170     public void descriptorCreated(Element element, SessionInfo session) throws IOException JavaDoc {
171     }
172
173     /* (non-Javadoc)
174      * @see com.sslexplorer.extensions.ExtensionType#getTypeBundle()
175      */

176     public String JavaDoc getTypeBundle() {
177         return "extensions";
178     }
179 }
Popular Tags