1 17 package org.apache.geronimo.plugin.packaging; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.ObjectInputStream ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.util.List ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 29 import org.apache.geronimo.gbean.GBeanData; 30 import org.apache.geronimo.gbean.GBeanInfo; 31 import org.apache.geronimo.gbean.GBeanInfoBuilder; 32 import org.apache.geronimo.kernel.config.Configuration; 33 import org.apache.geronimo.kernel.config.ConfigurationStore; 34 import org.apache.geronimo.kernel.config.InvalidConfigException; 35 import org.apache.geronimo.kernel.config.NoSuchConfigException; 36 import org.apache.geronimo.kernel.config.ConfigurationData; 37 import org.apache.geronimo.kernel.repository.Repository; 38 import org.apache.geronimo.kernel.Kernel; 39 40 47 public class MavenConfigStore implements ConfigurationStore { 48 private final Kernel kernel; 49 private final ObjectName objectName; 50 private final Repository repository; 51 52 public MavenConfigStore(Kernel kernel, String objectName, Repository repository) throws MalformedObjectNameException { 53 this.kernel = kernel; 54 this.objectName = new ObjectName (objectName); 55 this.repository = repository; 56 } 57 58 public String getObjectName() { 59 return objectName.toString(); 60 } 61 62 public synchronized ObjectName loadConfiguration(URI configId) throws NoSuchConfigException, IOException , InvalidConfigException { 63 if (!repository.hasURI(configId)) { 64 throw new NoSuchConfigException("Configuration not found: " + configId); 65 } 66 67 GBeanData config = new GBeanData(); 68 URL baseURL = new URL ("jar:" + repository.getURL(configId).toString() + "!/"); 69 InputStream jis = null; 70 try { 71 URL stateURL = new URL (baseURL, "META-INF/config.ser"); 72 jis = stateURL.openStream(); 73 ObjectInputStream ois = new ObjectInputStream (jis); 74 config.readExternal(ois); 75 config.setReferencePattern("ConfigurationStore", objectName); 76 } catch (ClassNotFoundException e) { 77 throw new InvalidConfigException("Unable to load class from config: " + configId, e); 78 } finally { 79 if (jis != null) { 80 jis.close(); 81 } 82 } 83 84 ObjectName name; 85 try { 86 name = Configuration.getConfigurationObjectName(configId); 87 } catch (MalformedObjectNameException e) { 88 throw new InvalidConfigException("Cannot convert id to ObjectName: ", e); 89 } 90 config.setName(name); 91 92 try { 93 kernel.loadGBean(config, Configuration.class.getClassLoader()); 94 } catch (Exception e) { 95 throw new InvalidConfigException("Unable to register configuration", e); 96 } 97 98 try { 99 kernel.setAttribute(name, "baseURL", baseURL); 100 } catch (Exception e) { 101 try { 102 kernel.unloadGBean(name); 103 } catch (Exception ignored) { 104 } 106 throw new InvalidConfigException("Cannot set baseURL", e); 107 } 108 109 return name; 110 } 111 112 public boolean containsConfiguration(URI configID) { 113 return repository.hasURI(configID); 114 } 115 116 public File createNewConfigurationDir() { 117 try { 118 File tmpFile = File.createTempFile("package", ".tmpdir"); 119 tmpFile.delete(); 120 tmpFile.mkdir(); 121 if (!tmpFile.isDirectory()) { 122 return null; 123 } 124 return tmpFile; 125 } catch (IOException e) { 126 return null; 128 } 129 } 130 131 public URI install(URL source) throws IOException , InvalidConfigException { 132 throw new UnsupportedOperationException (); 133 } 134 135 public void install(ConfigurationData configurationData, File source) throws IOException , InvalidConfigException { 136 throw new UnsupportedOperationException (); 137 } 138 139 public void uninstall(URI configID) throws NoSuchConfigException, IOException { 140 throw new UnsupportedOperationException (); 141 } 142 143 public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception { 144 } 146 147 public List listConfiguations() { 148 throw new UnsupportedOperationException (); 149 } 150 151 152 public static final GBeanInfo GBEAN_INFO; 153 154 public static GBeanInfo getGBeanInfo() { 155 return GBEAN_INFO; 156 } 157 158 static { 159 GBeanInfoBuilder builder = new GBeanInfoBuilder(MavenConfigStore.class); 160 builder.addInterface(ConfigurationStore.class); 161 builder.addAttribute("kernel", Kernel.class, false); 162 builder.addAttribute("objectName", String .class, false); 163 builder.addReference("Repository", Repository.class); 164 builder.setConstructor(new String []{"kernel", "objectName", "Repository"}); 165 GBEAN_INFO = builder.getBeanInfo(); 166 } 167 } 168 | Popular Tags |