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.Advertisement;
59 import net.jxta.document.AdvertisementFactory;
60 import net.jxta.document.XMLDocument;
61 import net.jxta.exception.ConfiguratorException;
62 import net.jxta.exception.JxtaError;
63 import net.jxta.impl.protocol.PSEConfigAdv;
64 import net.jxta.impl.protocol.PlatformConfig;
65 import net.jxta.logging.Logging;
66 import net.jxta.peergroup.PeerGroup;
68 import java.io.BufferedReader;
70 import java.io.IOException;
71 import java.io.InputStreamReader;
73 import java.util.NoSuchElementException;
74 import java.util.logging.Level;
75 import java.util.logging.Logger;
78 * This implementation provides the ability to reconfigure a JXTA PlatformConfig
79 * via an AWT based dialog. This is the original JXTA configuration mechanism.
81 public class DefaultConfigurator extends AutomaticConfigurator {
86 private final static transient Logger LOG = Logger.getLogger(DefaultConfigurator.class.getName());
89 * Configures the platform using the specified directory.
91 * @param jxtaHome store home URI
92 * @throws net.jxta.exception.ConfiguratorException
93 * if a configuration error occurs
95 public DefaultConfigurator(URI jxtaHome) throws ConfiguratorException {
102 * <p/>Kinda hackish in that we don't really do anything if home is not a file.
105 public boolean isReconfigure() {
106 if (!"file".equalsIgnoreCase(jxtaHome.getScheme())) {
110 File jxtaHomeDir = new File(jxtaHome);
112 boolean forceReconfig;
113 File file = new File(jxtaHomeDir, "reconf");
114 forceReconfig = file.exists();
116 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
117 LOG.fine("force reconfig : " + forceReconfig);
120 return forceReconfig;
121 } catch (Exception ex1) {
122 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
123 LOG.log(Level.SEVERE, "Could not check \'reconf\' file. Assuming it exists.", ex1);
125 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
126 LOG.config("Reconfig required - error getting \'reconf\' file");
136 public void setReconfigure(boolean reconfigure) {
137 if (!"file".equalsIgnoreCase(jxtaHome.getScheme())) {
141 File jxtaHomeDir = new File(jxtaHome);
142 File f = new File(jxtaHomeDir, "reconf");
147 } catch (IOException ex1) {
148 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
149 LOG.log(Level.SEVERE, "Could not create \'reconf\' file", ex1);
150 LOG.severe("Create the file \'reconf\' by hand before retrying.");
156 } catch (Exception ex1) {
157 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
158 LOG.log(Level.SEVERE, "Could not remove \'reconf\' file", ex1);
159 LOG.severe("Delete the file \'reconf\' by hand before retrying.");
169 public PlatformConfig getPlatformConfig() throws ConfiguratorException {
170 boolean needsConfig = isReconfigure();
172 if (needsConfig && Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
173 LOG.config("Reconfig requested - Forced reconfigure");
177 super.getPlatformConfig();
179 // Automatic configuration doesn't do any security config. We use
180 // this fact to decide if we must do configuration.
181 XMLDocument security = (XMLDocument) advertisement.getServiceParam(PeerGroup.membershipClassID);
183 if (null == security) {
186 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
187 LOG.config("Reconfig requested - No security info");
190 Advertisement adv = null;
193 adv = AdvertisementFactory.newAdvertisement(security);
194 } catch (NoSuchElementException notAnAdv) {// that's ok.
195 } catch (IllegalArgumentException badAdv) {// that's ok.
198 if (adv instanceof PSEConfigAdv) {
199 PSEConfigAdv pseConfig = (PSEConfigAdv) adv;
201 // no certificate? That means we need to make one.
202 needsConfig |= (null == pseConfig.getCertificate());
207 } catch (IncompleteConfigurationException configBad) {
212 setReconfigure(true);
215 if (java.awt.EventQueue.isDispatchThread()) {
216 LOG.severe("The JXTA AWT Configuration Dialog cannot be run from the event dispatch thread. It\'s modal nature is fundamentally incompatible with running on the event dispatch thread. Either change to a different Configurator via PeerGroupFactory.setConfiguratorClass() or start JXTA from the application main Thread before starting your GUI via invokeLater().");
217 // cruel but fair, the alternative is a UI deadlock.
220 ConfigDialog configUI = new ConfigDialog(advertisement);
222 configUI.untilDone();
223 setReconfigure(false);
224 } catch (Throwable t) {
225 if (t instanceof JxtaError) {
228 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
229 LOG.log(Level.WARNING, "Could not initialize graphical config dialog", t);
232 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
234 // clear any type-ahead
239 } catch (Exception ignored) {// ignored
244 System.out.println("The window-based configurator does not seem to be usable.");
245 System.out.print("Do you want to stop and edit the current configuration ? [no]: ");
247 String answer = "no";
250 answer = in.readLine();
251 } catch (Exception ignored) {// ignored
254 // this will cover all the cases of the answer yes, while
255 // allowing non-gui/batch type scripts to load the platform
256 // see platform issue #45
258 if ("yes".equalsIgnoreCase(answer)) {
260 System.out.println("Exiting; edit the file \"" + configFile.getPath()
261 + "\", remove the file \"reconf\", and then launch JXTA again.");
262 throw new JxtaError("Manual Configuration Requested");
264 System.out.println("Attempting to continue using the current configuration.");
268 return advertisement;