1 package com.sslexplorer.extensions.types; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.MalformedURLException ; 6 import java.net.URL ; 7 import java.util.Iterator ; 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 28 public class LanguagePackType implements ExtensionType { 29 30 final static Log log = LogFactory.getLog(LanguagePackType.class); 31 32 35 public final static String TYPE = "languagePack"; 36 37 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 String 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 packDefinition = new LanguagePackDefinition(descriptor, name); 55 LanguagePackManager.getInstance().addLanguagePackDefinition(packDefinition); 56 57 for (Iterator i = element.getChildren().iterator(); i.hasNext();) { 58 Element el = (Element) i.next(); 59 if (el.getName().equals("classpath")) { 60 String path = Util.trimmedBothOrBlank(el.getText()); 61 if (path != null && !path.equals("")) { 62 File f = new File (descriptor.getApplicationBundle().getBaseDir(), path); 63 if (f.exists()) { 64 try { 65 URL u = f.toURL(); 66 if (log.isInfoEnabled()) 67 log.info("Adding " + u + " to classpath"); 68 packDefinition.addClassPath(u); 69 } catch (MalformedURLException 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 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 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 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 if(!classpathAdded) { 104 for (Iterator j = packDefinition.getClassPath().iterator(); j.hasNext();) { 105 URL u = (URL ) j.next(); 106 ContextHolder.getContext().addContextLoaderURL(u); 107 } 108 classpathAdded = true; 109 } 110 } 111 } 112 113 118 public void verifyRequiredElements() throws ExtensionException { 119 } 120 121 126 public boolean isHidden() { 127 return true; 128 } 129 130 135 public String getType() { 136 return TYPE; 137 } 138 139 144 public void stop() throws ExtensionException { 145 if (packDefinition != null) { 146 LanguagePackManager.getInstance().removeLanguagePack(packDefinition); 147 } 148 } 149 150 155 public void activate() throws ExtensionException { 156 } 157 158 163 public boolean canStop() throws ExtensionException { 164 return true; 165 } 166 167 170 public void descriptorCreated(Element element, SessionInfo session) throws IOException { 171 } 172 173 176 public String getTypeBundle() { 177 return "extensions"; 178 } 179 } | Popular Tags |