KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > plugin > packaging > MavenConfigStore


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.plugin.packaging;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
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 /**
41  * Implementation of ConfigurationStore that loads Configurations from a repository.
42  * This implementation is read-only on the assumption that a separate maven task will
43  * handle installation of a built package into the repository.
44  *
45  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
46  */

47 public class MavenConfigStore implements ConfigurationStore {
48     private final Kernel kernel;
49     private final ObjectName JavaDoc objectName;
50     private final Repository repository;
51
52     public MavenConfigStore(Kernel kernel, String JavaDoc objectName, Repository repository) throws MalformedObjectNameException JavaDoc {
53         this.kernel = kernel;
54         this.objectName = new ObjectName JavaDoc(objectName);
55         this.repository = repository;
56     }
57
58     public String JavaDoc getObjectName() {
59         return objectName.toString();
60     }
61
62     public synchronized ObjectName JavaDoc loadConfiguration(URI JavaDoc configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
63         if (!repository.hasURI(configId)) {
64             throw new NoSuchConfigException("Configuration not found: " + configId);
65         }
66
67         GBeanData config = new GBeanData();
68         URL JavaDoc baseURL = new URL JavaDoc("jar:" + repository.getURL(configId).toString() + "!/");
69         InputStream JavaDoc jis = null;
70         try {
71             URL JavaDoc stateURL = new URL JavaDoc(baseURL, "META-INF/config.ser");
72             jis = stateURL.openStream();
73             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(jis);
74             config.readExternal(ois);
75             config.setReferencePattern("ConfigurationStore", objectName);
76         } catch (ClassNotFoundException JavaDoc 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 JavaDoc name;
85         try {
86             name = Configuration.getConfigurationObjectName(configId);
87         } catch (MalformedObjectNameException JavaDoc 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 JavaDoc e) {
95             throw new InvalidConfigException("Unable to register configuration", e);
96         }
97
98         try {
99             kernel.setAttribute(name, "baseURL", baseURL);
100         } catch (Exception JavaDoc e) {
101             try {
102                 kernel.unloadGBean(name);
103             } catch (Exception JavaDoc ignored) {
104                 // ignore
105
}
106             throw new InvalidConfigException("Cannot set baseURL", e);
107         }
108
109         return name;
110     }
111
112     public boolean containsConfiguration(URI JavaDoc configID) {
113         return repository.hasURI(configID);
114     }
115
116     public File JavaDoc createNewConfigurationDir() {
117         try {
118             File JavaDoc 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 JavaDoc e) {
126             // doh why can't I throw this?
127
return null;
128         }
129     }
130
131     public URI JavaDoc install(URL JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
132         throw new UnsupportedOperationException JavaDoc();
133     }
134
135     public void install(ConfigurationData configurationData, File JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
136         throw new UnsupportedOperationException JavaDoc();
137     }
138
139     public void uninstall(URI JavaDoc configID) throws NoSuchConfigException, IOException JavaDoc {
140         throw new UnsupportedOperationException JavaDoc();
141     }
142
143     public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception JavaDoc {
144         // we don't store persistent state
145
}
146
147     public List JavaDoc listConfiguations() {
148         throw new UnsupportedOperationException JavaDoc();
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 JavaDoc.class, false);
163         builder.addReference("Repository", Repository.class);
164         builder.setConstructor(new String JavaDoc[]{"kernel", "objectName", "Repository"});
165         GBEAN_INFO = builder.getBeanInfo();
166     }
167 }
168
Popular Tags