]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/peergroup/DefaultConfigurator.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / peergroup / DefaultConfigurator.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56 package net.jxta.impl.peergroup;
57
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;
67
68 import java.io.BufferedReader;
69 import java.io.File;
70 import java.io.IOException;
71 import java.io.InputStreamReader;
72 import java.net.URI;
73 import java.util.NoSuchElementException;
74 import java.util.logging.Level;
75 import java.util.logging.Logger;
76
77 /**
78  * This implementation provides the ability to reconfigure a JXTA PlatformConfig
79  * via an AWT based dialog. This is the original JXTA configuration mechanism.
80  */
81 public class DefaultConfigurator extends AutomaticConfigurator {
82
83     /**
84      * logger
85      */
86     private final static transient Logger LOG = Logger.getLogger(DefaultConfigurator.class.getName());
87
88     /**
89      * Configures the platform using the specified directory.
90      *
91      * @param jxtaHome store home URI
92      * @throws net.jxta.exception.ConfiguratorException
93      *          if a configuration error occurs
94      */
95     public DefaultConfigurator(URI jxtaHome) throws ConfiguratorException {
96         super(jxtaHome);
97     }
98
99     /**
100      * {@inheritDoc}
101      * <p/>
102      * <p/>Kinda hackish in that we don't really do anything if home is not a file.
103      */
104     @Override
105     public boolean isReconfigure() {
106         if (!"file".equalsIgnoreCase(jxtaHome.getScheme())) {
107             return false;
108         }
109
110         File jxtaHomeDir = new File(jxtaHome);
111         try {
112             boolean forceReconfig;
113             File file = new File(jxtaHomeDir, "reconf");
114             forceReconfig = file.exists();
115
116             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
117                 LOG.fine("force reconfig : " + forceReconfig);
118             }
119
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);
124             }
125             if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
126                 LOG.config("Reconfig required - error getting \'reconf\' file");
127             }
128             return true;
129         }
130     }
131
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public void setReconfigure(boolean reconfigure) {
137         if (!"file".equalsIgnoreCase(jxtaHome.getScheme())) {
138             return;
139         }
140
141         File jxtaHomeDir = new File(jxtaHome);
142         File f = new File(jxtaHomeDir, "reconf");
143
144         if (reconfigure) {
145             try {
146                 f.createNewFile();
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.");
151                 }
152             }
153         } else {
154             try {
155                 f.delete();
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.");
160                 }
161             }
162         }
163     }
164
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public PlatformConfig getPlatformConfig() throws ConfiguratorException {
170         boolean needsConfig = isReconfigure();
171
172         if (needsConfig && Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
173             LOG.config("Reconfig requested - Forced reconfigure");
174         }
175
176         try {
177             super.getPlatformConfig();
178
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);
182
183             if (null == security) {
184                 needsConfig = true;
185
186                 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
187                     LOG.config("Reconfig requested - No security info");
188                 }
189             } else {
190                 Advertisement adv = null;
191
192                 try {
193                     adv = AdvertisementFactory.newAdvertisement(security);
194                 } catch (NoSuchElementException notAnAdv) {// that's ok.
195                 } catch (IllegalArgumentException badAdv) {// that's ok.
196                 }
197
198                 if (adv instanceof PSEConfigAdv) {
199                     PSEConfigAdv pseConfig = (PSEConfigAdv) adv;
200
201                     // no certificate? That means we need to make one.
202                     needsConfig |= (null == pseConfig.getCertificate());
203                 } else {
204                     needsConfig = true;
205                 }
206             }
207         } catch (IncompleteConfigurationException configBad) {
208             needsConfig = true;
209         }
210
211         if (needsConfig) {
212             setReconfigure(true);
213
214             try {
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.
218                     System.exit(1);
219                 }
220                 ConfigDialog configUI = new ConfigDialog(advertisement);
221
222                 configUI.untilDone();
223                 setReconfigure(false);
224             } catch (Throwable t) {
225                 if (t instanceof JxtaError) {
226                     throw (JxtaError) t;
227                 }
228                 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
229                     LOG.log(Level.WARNING, "Could not initialize graphical config dialog", t);
230                 }
231
232                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
233
234                 // clear any type-ahead
235                 try {
236                     while (in.ready()) {
237                         in.readLine();
238                     }
239                 } catch (Exception ignored) {// ignored
240                 }
241
242                 System.err.flush();
243                 System.out.flush();
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]: ");
246                 System.out.flush();
247                 String answer = "no";
248
249                 try {
250                     answer = in.readLine();
251                 } catch (Exception ignored) {// ignored
252                 }
253
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
257
258                 if ("yes".equalsIgnoreCase(answer)) {
259                     save();
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");
263                 } else {
264                     System.out.println("Attempting to continue using the current configuration.");
265                 }
266             }
267         }
268         return advertisement;
269     }
270 }