2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
56 package net.jxta.impl.peergroup;
58 import net.jxta.document.AdvertisementFactory;
59 import net.jxta.document.MimeMediaType;
60 import net.jxta.document.StructuredDocumentFactory;
61 import net.jxta.document.XMLDocument;
62 import net.jxta.exception.ConfiguratorException;
63 import net.jxta.impl.protocol.PlatformConfig;
64 import net.jxta.logging.Logging;
65 import net.jxta.protocol.ConfigParams;
69 import java.util.logging.Level;
70 import java.util.logging.Logger;
73 * A minimal Platform Configurator. This implementation can load a
74 * configuration from an existing PlatformConfig file and also save a
75 * configuration to the PlatformConfig file.
77 * This configurator provides no explict validation of the PlatformConfig
78 * as it is read from the file (Some is done by the PlatformConfig class) and
79 * provides no mechanism for reconfiguration. The NullConfigurator provides a
80 * useful base implementation for extending your own Configurator and also
81 * provides the minimal implementation needed for applications which perform
82 * their own configuration.
84 public class NullConfigurator implements PlatformConfigurator {
89 private final static transient Logger LOG = Logger.getLogger(NullConfigurator.class.getName());
92 * The location in which the configuration files will reside.
94 protected final URI jxtaHome;
97 * The file in which contains the platform configurtation.
99 protected final URI configFile;
102 * The platform config
104 protected PlatformConfig advertisement = null;
107 * Constructor for the NullConfigurator
109 * @param homeRoot The location in which the configuration files will reside.
110 * @throws ConfiguratorException If there is a problem accessing the configuration information.
112 public NullConfigurator(URI homeRoot) throws ConfiguratorException {
113 if (!homeRoot.isAbsolute()) {
114 throw new IllegalArgumentException("homeRoot must be an absoluteURI");
119 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
120 LOG.config("JXTA_HOME = " + jxtaHome.toASCIIString());
123 if ("file".equalsIgnoreCase(jxtaHome.getScheme())) {
124 File jxtaHomeDir = new File(jxtaHome);
126 if (jxtaHomeDir.exists() && !jxtaHomeDir.isDirectory()) {
127 throw new IllegalArgumentException("'" + jxtaHomeDir + "' is not a directory.");
130 if (!jxtaHomeDir.exists()) {
131 if (!jxtaHomeDir.mkdirs()) {
132 throw new IllegalStateException("Could not create '" + jxtaHomeDir + "'.");
136 configFile = new File(jxtaHomeDir, "PlatformConfig").toURI();
138 configFile = jxtaHome.resolve("PlatformConfig");
145 public PlatformConfig getPlatformConfig() throws ConfiguratorException {
146 advertisement = (PlatformConfig) load();
148 return advertisement;
154 public final void setPlatformConfig(PlatformConfig config) {
155 advertisement = config;
161 public ConfigParams getConfigParams() throws ConfiguratorException {
162 return getPlatformConfig();
168 public void setConfigParams(ConfigParams cp) {
169 setPlatformConfig((PlatformConfig) cp);
175 public void setReconfigure(boolean reconfigure) {// This implementation doesn't do configuration so ignores this operation.
181 public boolean isReconfigure() {
188 public ConfigParams load() throws ConfiguratorException {
189 return load(configFile);
193 * Retrieves the persisted parameters associated with this configuration
194 * from the standard location.
196 * @param loadFile The location from which the configuration data should be
198 * @return The configuration parameters.
199 * @throws ConfiguratorException If there was a failure in retrieving the
200 * persisted parameters. This is normally a chained exception to the
202 * @deprecated Loading of existing configuration is best accomplished by use
203 * of specific constructors of the implementing configurator. This method
204 * complicates the state management of configuration parameters and may have
205 * unpredictable results depending upon the constructor and configuration
206 * set methods used prior to it's execution.
209 protected PlatformConfig load(URI loadFile) throws ConfiguratorException {
210 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
211 LOG.fine("Reading Platform Config from : " + loadFile);
214 InputStream advStream = null;
217 advStream = loadFile.toURL().openStream();
219 XMLDocument xmlDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, advStream);
220 PlatformConfig result = (PlatformConfig) AdvertisementFactory.newAdvertisement(xmlDoc);
222 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
223 LOG.fine("Recovered Platform Config from : " + loadFile);
227 } catch (FileNotFoundException e) {
228 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
229 LOG.warning("Platform Config not found : " + loadFile);
233 } catch (Exception e) {
234 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
235 LOG.log(Level.WARNING, "Failed to Recover \'" + loadFile + "\' due to : ", e);
238 throw new ConfiguratorException("Failed to recover PlatformConfig", e);
241 if (advStream != null) {
245 } catch (Exception ignored) {// ignored
253 public boolean save() throws ConfiguratorException {
254 return save(configFile);
258 * Persist the parameters associated with this configuration to the
259 * specified location.
261 * @param saveFile The location to which the configuration should be saved.
262 * @return <code>true</code> if the configuration was successfully saved
263 * otherwise <code>false</code>. If the parameters are not persisted then
264 * <code>false/code> is returned.
265 * @throws ConfiguratorException If there was a failure in persisting the
266 * parameters. This is normally a chained exception to the underlying
269 protected boolean save(URI saveFile) throws ConfiguratorException {
271 // Save the adv as input for future reconfiguration
272 OutputStream out = null;
275 XMLDocument aDoc = (XMLDocument) advertisement.getDocument(MimeMediaType.XMLUTF8);
277 if ("file".equalsIgnoreCase(saveFile.getScheme())) {
278 out = new FileOutputStream(new File(saveFile));
280 out = saveFile.toURL().openConnection().getOutputStream();
283 OutputStreamWriter os = new OutputStreamWriter(out, "UTF-8");
285 aDoc.sendToWriter(os);
287 } catch (IOException e) {
288 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
289 LOG.log(Level.WARNING, "Could not save to : " + saveFile, e);
292 throw new ConfiguratorException("Could not save to : " + saveFile, e);
298 } catch (Exception ignored) {// ignored