Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / Launcher.java @ 1381

History | View | Annotate | Download (38.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.andami;
42

    
43
import com.iver.andami.config.generate.Andami;
44
import com.iver.andami.config.generate.AndamiConfig;
45
import com.iver.andami.config.generate.Plugin;
46
import com.iver.andami.messages.Messages;
47
import com.iver.andami.messages.NotificationManager;
48
import com.iver.andami.plugins.PluginClassLoader;
49
import com.iver.andami.plugins.config.generate.ActionTool;
50
import com.iver.andami.plugins.config.generate.Depends;
51
import com.iver.andami.plugins.config.generate.Extension;
52
import com.iver.andami.plugins.config.generate.Extensions;
53
import com.iver.andami.plugins.config.generate.LabelSet;
54
import com.iver.andami.plugins.config.generate.Menu;
55
import com.iver.andami.plugins.config.generate.PluginConfig;
56
import com.iver.andami.plugins.config.generate.PopupMenu;
57
import com.iver.andami.plugins.config.generate.PopupMenus;
58
import com.iver.andami.plugins.config.generate.SelectableTool;
59
import com.iver.andami.plugins.config.generate.SkinExtension;
60
import com.iver.andami.plugins.config.generate.SkinExtensionType;
61
import com.iver.andami.plugins.config.generate.ToolBar;
62
import com.iver.andami.ui.AndamiEventQueue;
63
import com.iver.andami.ui.MDIManagerLoadException;
64
import com.iver.andami.ui.SplashWindow;
65
import com.iver.andami.ui.mdiFrame.MDIFrame;
66
import com.iver.andami.ui.mdiManager.MDIManagerFactory;
67

    
68
import com.iver.utiles.XMLEntity;
69
import com.iver.utiles.xmlEntity.generate.XmlTag;
70

    
71
import org.apache.log4j.Logger;
72
import org.apache.log4j.PatternLayout;
73
import org.apache.log4j.PropertyConfigurator;
74
import org.apache.log4j.RollingFileAppender;
75

    
76
import org.exolab.castor.xml.MarshalException;
77
import org.exolab.castor.xml.ValidationException;
78

    
79
import java.awt.EventQueue;
80
import java.awt.Toolkit;
81

    
82
import java.io.BufferedInputStream;
83
import java.io.BufferedOutputStream;
84
import java.io.File;
85
import java.io.FileFilter;
86
import java.io.FileInputStream;
87
import java.io.FileNotFoundException;
88
import java.io.FileOutputStream;
89
import java.io.FileReader;
90
import java.io.FileWriter;
91
import java.io.FilenameFilter;
92
import java.io.IOException;
93
import java.io.InputStream;
94

    
95
import java.lang.reflect.InvocationTargetException;
96

    
97
import java.net.MalformedURLException;
98
import java.net.URL;
99
import java.net.URLConnection;
100

    
101
import java.util.ArrayList;
102
import java.util.Comparator;
103
import java.util.Date;
104
import java.util.HashMap;
105
import java.util.HashSet;
106
import java.util.Iterator;
107
import java.util.Locale;
108
import java.util.Properties;
109
import java.util.PropertyResourceBundle;
110
import java.util.ResourceBundle;
111
import java.util.TreeMap;
112

    
113
import javax.jnlp.BasicService;
114
import javax.jnlp.ServiceManager;
115
import javax.jnlp.UnavailableServiceException;
116

    
117
import javax.swing.ImageIcon;
118
import javax.swing.JComponent;
119
import javax.swing.SwingUtilities;
120
import javax.swing.UIManager;
121

    
122

    
123
/**
124
 * DOCUMENT ME!
125
 *
126
 * @author $author$
127
 * @version $Revision: 1381 $
128
 */
129
public class Launcher {
130
        private static Logger logger = Logger.getLogger(Launcher.class.getName());
131
        private static AndamiConfig andamiConfig;
132
        private static SplashWindow splashWindow;
133
        private static String userHome = System.getProperty("user.home");
134
        private static String appName;
135
        private static Locale locale;
136
        private static HashMap pluginsConfig = new HashMap();
137
        private static HashMap pluginsServices = new HashMap();
138
        private static MDIFrame frame;
139
        private static HashMap classesExtensions = new HashMap();
140
        private static String andamiConfigPath;
141
        private static String pluginsPersistencePath;
142

    
143
        /**
144
         * DOCUMENT ME!
145
         *
146
         * @param args DOCUMENT ME!
147
         *
148
         * @throws InterruptedException
149
         * @throws InvocationTargetException
150
         * @throws ConfigurationException
151
         * @throws MDIManagerLoadException
152
         * @throws IOException
153
         */
154
        public static void main(String[] args)
155
                throws InterruptedException, InvocationTargetException, 
156
                        ConfigurationException, MDIManagerLoadException, IOException {
157
                if ((args.length < 1) || (args.length > 3)) {
158
                        System.err.println("Uso: Launcher appName plugins-directory [locale]");
159
                }
160

    
161
                appName = args[0];
162

    
163
                //Se crea el directorio de configuraci?n de la aplicaci?n
164
                File parent = new File(System.getProperty("user.home") +
165
                                File.separator + args[0] + File.separator);
166
                parent.mkdirs();
167

    
168
                andamiConfigPath = System.getProperty("user.home") + File.separator +
169
                        appName + File.separator + "andami-config.xml";
170
                pluginsPersistencePath = System.getProperty("user.home") +
171
                        File.separator + appName + File.separator +
172
                        "plugins-persistence.xml";
173

    
174
                // Configurar el log4j
175
                PropertyConfigurator.configure(Launcher.class.getClassLoader()
176
                                                                                                         .getResource("log4j.properties"));
177

    
178
                PatternLayout l = new PatternLayout("%p %t %C - %m%n");
179
                RollingFileAppender fa = new RollingFileAppender(l,
180
                                System.getProperty("user.home") + File.separator + args[0] +
181
                                File.separator + args[0] + ".log", false);
182
                fa.setMaxFileSize("512KB");
183
                fa.setMaxBackupIndex(3);
184
                Logger.getRootLogger().addAppender(fa);
185

    
186
                // Leer el fichero de configuraci?n de andami (andami-config.xsd)
187
                // locale
188
                // Buscar actualizaci?nes al comenzar
189
                //  Andami
190
                //  Plugins
191
                // Directorio de las extensiones
192
                andamiConfigFromXML(andamiConfigPath);
193
                andamiConfig.setPluginsDirectory(args[1]);
194

    
195
                // Configurar el locale
196
                if (args.length == 3) {
197
                        locale = new Locale(args[2]);
198
                } else {
199
                        locale = getLocale(andamiConfig.getLocaleLanguage(),
200
                                        andamiConfig.getLocaleCountry(),
201
                                        andamiConfig.getLocaleVariant());
202
                }
203

    
204
                Locale.setDefault(locale);
205
                JComponent.setDefaultLocale(locale);
206
                Messages.init(locale);
207

    
208
                //Se pone el lookAndFeel
209
                try {
210
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
211
                } catch (Exception e) {
212
                        logger.warn(Messages.getString("Launcher.look_and_feel"), e);
213
                }
214

    
215
                // Mostrar la ventana de inicio
216
                splashWindow = new SplashWindow(null);
217

    
218
                // TODO Buscar actualizaciones de Andami
219
                // TODO Buscar actualizaciones de los plugins
220
                downloadExtensions(andamiConfig.getPluginsDirectory());
221

    
222
                // Se leen los config.xml de los plugins -----++++
223
                loadPlugins(andamiConfig.getPluginsDirectory());
224

    
225
                // Se configura el classloader del plugin
226
                pluginsClassLoaders();
227

    
228
                // Se carga un Skin si alguno de los plugins trae informaci?n para ello
229
                skinPlugin();
230

    
231
                //Se configura la cola de eventos
232
                EventQueue waitQueue = new AndamiEventQueue();
233
                Toolkit.getDefaultToolkit().getSystemEventQueue().push(waitQueue);
234

    
235
                // Se configura la mensajer?a del plugin
236
                pluginsMessages();
237

    
238
                // Se modifica el andami-config con los plugins nuevos
239
                updateAndamiConfig();
240

    
241
                // Se prepara el MainFrame para albergar las extensiones
242
                frame = new MDIFrame();
243

    
244
                // Se configura el nombre e icono de la aplicaci?n
245
                frameIcon();
246

    
247
                SwingUtilities.invokeAndWait(new Runnable() {
248
                                public void run() {
249
                                        frame.init();
250
                                }
251
                        });
252

    
253
                // Se instalan los controles de las extensiones de los plugins
254
                SwingUtilities.invokeAndWait(new Runnable() {
255
                                public void run() {
256
                                        installPluginsControls();
257
                                        installPluginsMenus();
258
                                        installPluginsLabels();
259
                                }
260
                        });
261

    
262
                // Leer el fichero de persistencia
263
                //  info de los plugins
264
                //  bookmarks de los plugins
265
                loadPluginsPersistence();
266

    
267
                // Se instalan los controles del skin
268
                // Se inicializan todas las extensiones de todos los plugins
269
                SwingUtilities.invokeAndWait(new Runnable() {
270
                                public void run() {
271
                                        initializeExtensions();
272
                                }
273
                        });
274
                frame.setClassesExtensions(classesExtensions);
275

    
276
                // Se instalan los bookmarks de los plugins
277
                splashWindow.close();
278

    
279
                //Se muestra el frame principal
280
                frame.show();
281

    
282
                SwingUtilities.invokeAndWait(new Runnable() {
283
                                public void run() {
284
                                        frame.enableControls();
285
                                }
286
                        });
287
        }
288

    
289
        /**
290
         * DOCUMENT ME!
291
         *
292
         * @throws ConfigurationException
293
         */
294
        private static void loadPluginsPersistence() throws ConfigurationException {
295
                XMLEntity entity = persistenceFromXML();
296

    
297
                for (int i = 0; i < entity.getNumChild(); i++) {
298
                        XMLEntity plugin = entity.getChild(i);
299
                        String pName = plugin.getStringProperty(
300
                                        "com.iver.andami.pluginName");
301
                        if (pluginsServices.get(pName)!= null){
302
                                ((PluginServices) pluginsServices.get(pName)).setPersistentXML(plugin);
303
                        }
304
                }
305
        }
306

    
307
        /**
308
         * DOCUMENT ME!
309
         */
310
        private static void installPluginsLabels() {
311
                Iterator i = pluginsConfig.keySet().iterator();
312

    
313
                while (i.hasNext()) {
314
                        String name = (String) i.next();
315
                        PluginConfig pc = (PluginConfig) pluginsConfig.get(name);
316
                        PluginServices ps = (PluginServices) pluginsServices.get(name);
317

    
318
                        LabelSet[] ls = pc.getLabelSet();
319

    
320
                        for (int j = 0; j < ls.length; j++) {
321
                                PluginClassLoader loader = ps.getClassLoader();
322

    
323
                                try {
324
                                        Class clase = loader.loadClass(ls[j].getClassName());
325
                                        frame.setLabels(clase, ls[j].getLabel());
326
                                } catch (ClassNotFoundException e) {
327
                                        logger.error(Messages.getString("Launcher.labelset_class"),
328
                                                e);
329
                                }
330
                        }
331
                }
332
        }
333

    
334
        /**
335
         * DOCUMENT ME!
336
         *
337
         * @throws MDIManagerLoadException
338
         */
339
        private static void skinPlugin() throws MDIManagerLoadException {
340
                Iterator i = pluginsConfig.keySet().iterator();
341

    
342
                while (i.hasNext()) {
343
                        String name = (String) i.next();
344
                        PluginConfig pc = (PluginConfig) pluginsConfig.get(name);
345
                        PluginServices ps = (PluginServices) pluginsServices.get(name);
346

    
347
                        if (pc.getExtensions().getSkinExtension() != null) {
348
                                if (MDIManagerFactory.getSkinExtension() != null) {
349
                                        logger.warn(Messages.getString(
350
                                                        "Launcher.Dos_skin_extension"));
351
                                }
352

    
353
                                SkinExtension se = pc.getExtensions().getSkinExtension();
354

    
355
                                MDIManagerFactory.setSkinExtension(se, ps.getClassLoader());
356

    
357
                                Class skinClass;
358

    
359
                                try {
360
                                        skinClass = ps.getClassLoader().loadClass(se.getClassName());
361

    
362
                                        com.iver.andami.plugins.Extension skinInstance = (com.iver.andami.plugins.Extension) skinClass.newInstance();
363
                                        classesExtensions.put(skinClass, skinInstance);
364
                                } catch (ClassNotFoundException e) {
365
                                        logger.error(Messages.getString(
366
                                                        "Launcher.No_se_encontro_la_clase_mdi_manager"), e);
367
                                        throw new MDIManagerLoadException(e);
368
                                } catch (InstantiationException e) {
369
                                        logger.error(Messages.getString(
370
                                                        "Launcher.No_se_pudo_instanciar_la_clase_mdi_manager"),
371
                                                e);
372
                                        throw new MDIManagerLoadException(e);
373
                                } catch (IllegalAccessException e) {
374
                                        logger.error(Messages.getString(
375
                                                        "Launcher.No_se_pudo_acceder_a_la_clase_mdi_manager"),
376
                                                e);
377
                                        throw new MDIManagerLoadException(e);
378
                                }
379
                        }
380
                }
381
        }
382

    
383
        /**
384
         *
385
         */
386
        private static void frameIcon() {
387
                Iterator i = pluginsConfig.keySet().iterator();
388

    
389
                while (i.hasNext()) {
390
                        String pName = (String) i.next();
391
                        PluginConfig pc = (PluginConfig) pluginsConfig.get(pName);
392
                        PluginServices ps = (PluginServices) pluginsServices.get(pName);
393

    
394
                        if (pc.getIcon() != null) {
395
                                ImageIcon icon = new ImageIcon(ps.getClassLoader().getResource(pc.getIcon()
396
                                                                                                                                                                 .getSrc()));
397
                                frame.setIconImage(icon.getImage());
398
                                frame.setTitle(pc.getIcon().getText());
399
                        }
400
                }
401
        }
402

    
403
        /**
404
         *
405
         */
406
        private static void initializeExtensions() {
407
                Iterator i = pluginsConfig.keySet().iterator();
408

    
409
                while (i.hasNext()) {
410
                        String pName = (String) i.next();
411
                        PluginConfig pc = (PluginConfig) pluginsConfig.get(pName);
412
                        PluginServices ps = (PluginServices) pluginsServices.get(pName);
413

    
414
                        Extension[] exts = pc.getExtensions().getExtension();
415

    
416
                        TreeMap orderedExtensions = new TreeMap(new ExtensionComparator());
417

    
418
                        for (int j = 0; j < exts.length; j++) {
419
                                if (!exts[j].getActive()) {
420
                                        continue;
421
                                }
422

    
423
                                if (orderedExtensions.containsKey(exts[j])) {
424
                                        logger.warn(Messages.getString(
425
                                                        "Launcher.Two_extensions_with_the_same_priority") +
426
                                                exts[j].getClassName());
427
                                }
428

    
429
                                orderedExtensions.put(exts[j], null);
430
                        }
431

    
432
                        Iterator e = orderedExtensions.keySet().iterator();
433

    
434
                        while (e.hasNext()) {
435
                                Extension extension = (Extension) e.next();
436
                                com.iver.andami.plugins.Extension extensionInstance;
437

    
438
                                try {
439
                                        Class extensionClass = ps.getClassLoader().loadClass(extension.getClassName());
440
                                        extensionInstance = (com.iver.andami.plugins.Extension) extensionClass.newInstance();
441
                                        classesExtensions.put(extensionClass, extensionInstance);
442

    
443
                                        extensionInstance.inicializar();
444
                                } catch (InstantiationException e1) {
445
                                        logger.error(Messages.getString(
446
                                                        "Launcher.Error_instanciando_la_extension") +
447
                                                extension.getClassName(), e1);
448
                                } catch (IllegalAccessException e1) {
449
                                        logger.error(Messages.getString(
450
                                                        "Launcher.Error_instanciando_la_extension") +
451
                                                extension.getClassName(), e1);
452
                                } catch (ClassNotFoundException e1) {
453
                                        logger.error(Messages.getString(
454
                                                        "Launcher.No_se_encontro_la_clase_de_la_extension") +
455
                                                extension.getClassName(), e1);
456
                                } catch (NoClassDefFoundError e1) {
457
                                        logger.error(Messages.getString(
458
                                                        "Launcher.Error_localizando_la_clase_de_la_extension") +
459
                                                extension.getClassName(), e1);
460
                                }
461
                        }
462
                }
463
        }
464

    
465
        /**
466
         * DOCUMENT ME!
467
         */
468
        private static void installPluginsMenus() {
469
                TreeMap orderedMenus = new TreeMap(new MenuComparator());
470

    
471
                Iterator i = pluginsConfig.keySet().iterator();
472

    
473
                while (i.hasNext()) {
474
                        String pName = (String) i.next();
475
                        PluginServices ps = (PluginServices) pluginsServices.get(pName);
476
                        PluginConfig pc = (PluginConfig) pluginsConfig.get(pName);
477

    
478
                        Extension[] exts = pc.getExtensions().getExtension();
479

    
480
                        for (int j = 0; j < exts.length; j++) {
481
                                if (!exts[j].getActive()) {
482
                                        continue;
483
                                }
484

    
485
                                Menu[] menu = exts[j].getMenu();
486

    
487
                                for (int k = 0; k < menu.length; k++) {
488
                                        SortableMenu sm = new SortableMenu(ps.getClassLoader(),
489
                                                        exts[j], menu[k]);
490

    
491
                                        if (orderedMenus.containsKey(sm)) {
492
                                                logger.error(Messages.getString(
493
                                                                "Launcher.Two_menus_with_the_same_position") +
494
                                                        exts[j].getClassName());
495
                                        }
496

    
497
                                        orderedMenus.put(sm, null);
498
                                }
499
                        }
500

    
501
                        // Se instalan las extensiones de MDI
502
                        SkinExtension skinExt = pc.getExtensions().getSkinExtension();
503

    
504
                        if (skinExt != null) {
505
                                Menu[] menu = skinExt.getMenu();
506

    
507
                                for (int k = 0; k < menu.length; k++) {
508
                                        SortableMenu sm = new SortableMenu(ps.getClassLoader(),
509
                                                        skinExt, menu[k]);
510

    
511
                                        if (orderedMenus.containsKey(sm)) {
512
                                                logger.error(Messages.getString(
513
                                                                "Launcher.Two_menus_with_the_same_position") +
514
                                                        skinExt.getClassName());
515
                                        }
516

    
517
                                        orderedMenus.put(sm, null);
518
                                }
519
                        }
520
                }
521

    
522
                //Se itera por los menus ordenados
523
                Iterator e = orderedMenus.keySet().iterator();
524

    
525
                // Se ordenan los menues
526
                while (e.hasNext()) {
527
                        try {
528
                                SortableMenu sm = (SortableMenu) e.next();
529

    
530
                                frame.addMenu(sm.loader, sm.extension, sm.menu);
531
                        } catch (ClassNotFoundException ex) {
532
                                logger.error(Messages.getString(
533
                                                "Launcher.No_se_encontro_la_clase_de_la_extension"), ex);
534
                        }
535
                }
536
        }
537

    
538
        /**
539
         *
540
         */
541
        private static void installPluginsControls() {
542
                Iterator i = pluginsConfig.keySet().iterator();
543

    
544
                while (i.hasNext()) {
545
                        try {
546
                                String pName = (String) i.next();
547
                                PluginConfig pc = (PluginConfig) pluginsConfig.get(pName);
548
                                PluginServices ps = (PluginServices) pluginsServices.get(pName);
549

    
550
                                Extension[] exts = pc.getExtensions().getExtension();
551

    
552
                                TreeMap orderedExtensions = new TreeMap(new ExtensionComparator());
553

    
554
                                for (int j = 0; j < exts.length; j++) {
555
                                        if (exts[j].getActive()) {
556
                                                if (orderedExtensions.containsKey(exts[j])) {
557
                                                        logger.error(Messages.getString(
558
                                                                        "Launcher.Two_extensions_with_the_same_priority") +
559
                                                                exts[j].getClassName());
560
                                                }
561

    
562
                                                orderedExtensions.put(exts[j], null);
563
                                        }
564
                                }
565

    
566
                                Iterator e = orderedExtensions.keySet().iterator();
567

    
568
                                // Se instalan las extensiones
569
                                while (e.hasNext()) {
570
                                        Extension ext = (Extension) e.next();
571

    
572
                                        ToolBar[] toolbars = ext.getToolBar();
573

    
574
                                        for (int k = 0; k < toolbars.length; k++) {
575
                                                ActionTool[] tools = toolbars[k].getActionTool();
576

    
577
                                                for (int t = 0; t < tools.length; t++) {
578
                                                        frame.addTool(ps.getClassLoader(), ext,
579
                                                                toolbars[k], tools[t]);
580
                                                }
581

    
582
                                                SelectableTool[] sTools = toolbars[k].getSelectableTool();
583

    
584
                                                for (int t = 0; t < sTools.length; t++) {
585
                                                        frame.addTool(ps.getClassLoader(), ext,
586
                                                                toolbars[k], sTools[t]);
587
                                                }
588
                                        }
589

    
590
                                        /*
591
                                           Menu[] menu = ext.getMenu();
592
                                           for (int k = 0; k < menu.length; k++) {
593
                                               frame.addMenu(ps.getClassLoader(), ext, menu[k]);
594
                                           }
595
                                         */
596
                                }
597

    
598
                                // Se instalan las extensiones de MDI
599
                                SkinExtension skinExt = pc.getExtensions().getSkinExtension();
600

    
601
                                if (skinExt != null) {
602
                                        ToolBar[] toolbars = skinExt.getToolBar();
603

    
604
                                        for (int k = 0; k < toolbars.length; k++) {
605
                                                ActionTool[] tools = toolbars[k].getActionTool();
606

    
607
                                                for (int t = 0; t < tools.length; t++) {
608
                                                        frame.addTool(ps.getClassLoader(), skinExt,
609
                                                                toolbars[k], tools[t]);
610
                                                }
611

    
612
                                                SelectableTool[] sTools = toolbars[k].getSelectableTool();
613

    
614
                                                for (int t = 0; t < sTools.length; t++) {
615
                                                        frame.addTool(ps.getClassLoader(), skinExt,
616
                                                                toolbars[k], sTools[t]);
617
                                                }
618
                                        }
619

    
620
                                        /*
621
                                           Menu[] menu = skinExt.getMenu();
622
                                           for (int k = 0; k < menu.length; k++) {
623
                                               frame.addMenu(ps.getClassLoader(), skinExt, menu[k]);
624
                                           }
625
                                         */
626
                                }
627

    
628
                                //Se instalan los popup menus
629
                                PopupMenus pus = pc.getPopupMenus();
630

    
631
                                if (pus != null) {
632
                                        PopupMenu[] menus = pus.getPopupMenu();
633

    
634
                                        for (int j = 0; j < menus.length; j++) {
635
                                                frame.addPopupMenu(ps.getClassLoader(), menus[j]);
636
                                        }
637
                                }
638
                        } catch (ClassNotFoundException e) {
639
                                logger.error(Messages.getString(
640
                                                "Launcher.No_se_encontro_la_clase_de_la_extension"), e);
641
                        }
642
                }
643
        }
644

    
645
        /**
646
         *
647
         */
648
        private static void updateAndamiConfig() {
649
                HashSet olds = new HashSet();
650

    
651
                Plugin[] plugins = andamiConfig.getPlugin();
652

    
653
                for (int i = 0; i < plugins.length; i++) {
654
                        olds.add(plugins[i].getName());
655
                }
656

    
657
                Iterator i = pluginsServices.values().iterator();
658

    
659
                while (i.hasNext()) {
660
                        PluginServices ps = (PluginServices) i.next();
661

    
662
                        if (!olds.contains(ps.getPluginName())) {
663
                                Plugin p = new Plugin();
664
                                p.setName(ps.getPluginName());
665
                                p.setUpdate(false);
666

    
667
                                andamiConfig.addPlugin(p);
668
                        }
669
                }
670
        }
671

    
672
        /**
673
         * DOCUMENT ME!
674
         */
675
        private static void pluginsClassLoaders() {
676
                HashSet instalados = new HashSet();
677

    
678
                // Se itera hasta que est?n todos instalados
679
                while (instalados.size() != pluginsConfig.size()) {
680
                        boolean circle = true;
681

    
682
                        //Hacemos una pasada por todos los plugins
683
                        Iterator i = pluginsConfig.keySet().iterator();
684

    
685
                        while (i.hasNext()) {
686
                                String pluginName = (String) i.next();
687
                                PluginConfig config = (PluginConfig) pluginsConfig.get(pluginName);
688

    
689
                                if (instalados.contains(pluginName)) {
690
                                        continue;
691
                                }
692

    
693
                                //Se obtienen las dependencias y sus class loaders
694
                                boolean ready = true;
695
                                Depends[] dependencies = config.getDepends();
696
                                PluginClassLoader[] loaders = new PluginClassLoader[dependencies.length];
697

    
698
                                for (int j = 0; j < dependencies.length; j++) {
699
                                        if (pluginsConfig.get(dependencies[j].getPluginName()) == null) {
700
                                                logger.error(Messages.getString(
701
                                                                "Launcher.Dependencia_no_resuelta_en_plugin") +
702
                                                        pluginName + ": " +
703
                                                        dependencies[j].getPluginName());
704

    
705
                                                continue;
706
                                        }
707

    
708
                                        if (!instalados.contains(dependencies[j].getPluginName())) {
709
                                                ready = false;
710
                                        } else {
711
                                                loaders[j] = ((PluginServices) pluginsServices.get(dependencies[j].getPluginName())).getClassLoader();
712
                                        }
713
                                }
714

    
715
                                //Si no est?n sus dependencias satisfechas se aborta la instalaci?n
716
                                if (!ready) {
717
                                        continue;
718
                                }
719

    
720
                                //Se genera el class loader
721
                                String jardir = config.getLibraries().getLibraryDir();
722
                                File jarDir = new File(andamiConfig.getPluginsDirectory() +
723
                                                File.separator + pluginName + File.separator + jardir);
724
                                File[] jarFiles = jarDir.listFiles(new FileFilter() {
725
                                                        public boolean accept(File pathname) {
726
                                                                return (pathname.getName().toUpperCase()
727
                                                                                                .endsWith(".JAR")) ||
728
                                                                (pathname.getName().toUpperCase().endsWith(".ZIP"));
729
                                                        }
730
                                                });
731

    
732
                                URL[] urls = new URL[jarFiles.length];
733

    
734
                                for (int j = 0; j < jarFiles.length; j++) {
735
                                        try {
736
                                                urls[j] = new URL("file:" + jarFiles[j]);
737
                                        } catch (MalformedURLException e) {
738
                                                logger.error(Messages.getString(
739
                                                                "Launcher.No_se_puede_acceder_a") +
740
                                                        jarFiles[j]);
741
                                        }
742
                                }
743

    
744
                                PluginClassLoader loader;
745

    
746
                                try {
747
                                        loader = new PluginClassLoader(urls,
748
                                                        andamiConfig.getPluginsDirectory() +
749
                                                        File.separator + pluginName,
750
                                                        Launcher.class.getClassLoader(), loaders);
751

    
752
                                        PluginServices ps = new PluginServices(loader);
753

    
754
                                        pluginsServices.put(ps.getPluginName(), ps);
755

    
756
                                        instalados.add(pluginName);
757

    
758
                                        circle = false;
759
                                } catch (IOException e) {
760
                                        logger.error(Messages.getString(
761
                                                        "Launcher.Error_con_las_librerias_del_plugin"), e);
762
                                        pluginsConfig.remove(pluginName);
763
                                        i = pluginsConfig.keySet().iterator();
764
                                }
765
                        }
766

    
767
                        if (circle) {
768
                                logger.error(Messages.getString(
769
                                                "Launcher.Hay_dependencias_circulares"));
770

    
771
                                break;
772
                        }
773
                }
774

    
775
                //Se eliminan los plugins que no fueron instalados
776
                Iterator i = pluginsConfig.keySet().iterator();
777

    
778
                while (i.hasNext()) {
779
                        String pluginName = (String) i.next();
780
                        PluginConfig config = (PluginConfig) pluginsConfig.get(pluginName);
781
                        PluginServices ps = (PluginServices) pluginsServices.get(pluginName);
782

    
783
                        if (ps == null) {
784
                                pluginsConfig.remove(pluginName);
785
                                i = pluginsConfig.keySet().iterator();
786
                        }
787
                }
788
        }
789

    
790
        /**
791
         * DOCUMENT ME!
792
         */
793
        private static void pluginsMessages() {
794
                //Iteramos por todos los plugins
795
                Iterator i = pluginsConfig.keySet().iterator();
796

    
797
                while (i.hasNext()) {
798
                        String pluginName = (String) i.next();
799
                        PluginConfig config = (PluginConfig) pluginsConfig.get(pluginName);
800

    
801
                        PluginServices ps = (PluginServices) pluginsServices.get(pluginName);
802

    
803
                        if (config.getResourceBundle() != null) {
804
                                ps.setResourceBundle(config.getResourceBundle().getName(),
805
                                        locale);
806
                        }
807
                }
808
        }
809

    
810
        /**
811
         * DOCUMENT ME!
812
         *
813
         * @param name DOCUMENT ME!
814
         *
815
         * @return DOCUMENT ME!
816
         */
817
        static PluginServices getPluginServices(String name) {
818
                return (PluginServices) pluginsServices.get(name);
819
        }
820

    
821
        /**
822
         * DOCUMENT ME!
823
         *
824
         * @return DOCUMENT ME!
825
         */
826
        static String getPluginsDir() {
827
                return andamiConfig.getPluginsDirectory();
828
        }
829

    
830
        /**
831
         * DOCUMENT ME!
832
         *
833
         * @param s DOCUMENT ME!
834
         */
835
        static void setPluginsDir(String s) {
836
                andamiConfig.setPluginsDirectory(s);
837
        }
838

    
839
        /**
840
         * DOCUMENT ME!
841
         *
842
         * @return DOCUMENT ME!
843
         */
844
        static MDIFrame getMDIFrame() {
845
                return frame;
846
        }
847

    
848
        /**
849
         * DOCUMENT ME!
850
         *
851
         * @param pluginsDirectory
852
         */
853
        private static void loadPlugins(String pluginsDirectory) {
854
                File pDir = new File(pluginsDirectory);
855

    
856
                if (!pDir.exists()) {
857
                        return;
858
                }
859

    
860
                File[] pluginDirs = pDir.listFiles();
861

    
862
                for (int i = 0; i < pluginDirs.length; i++) {
863
                        if (pluginDirs[i].isDirectory()) {
864
                                File configXml = new File(pluginDirs[i].getAbsolutePath() +
865
                                                File.separator + "config.xml");
866

    
867
                                try {
868
                                        FileReader xml = new FileReader(configXml);
869
                                        PluginConfig pConfig = (PluginConfig) PluginConfig.unmarshal(xml);
870
                                        pluginsConfig.put(pluginDirs[i].getName(), pConfig);
871
                                } catch (FileNotFoundException e) {
872
                                        logger.info(Messages.getString(
873
                                                        "Launcher.Ignorando_el_directorio") +
874
                                                pluginDirs[i].getAbsolutePath() +
875
                                                Messages.getString("Launcher.config_no_encontrado"));
876
                                } catch (MarshalException e) {
877
                                        logger.info(Messages.getString(
878
                                                        "Launcher.Ignorando_el_directorio") +
879
                                                pluginDirs[i].getAbsolutePath() +
880
                                                Messages.getString("Launcher.config_mal_formado"), e);
881
                                } catch (ValidationException e) {
882
                                        logger.info(Messages.getString(
883
                                                        "Launcher.Ignorando_el_directorio") +
884
                                                pluginDirs[i].getAbsolutePath() +
885
                                                Messages.getString("Launcher.config_mal_formado"), e);
886
                                }
887
                        }
888
                }
889
        }
890

    
891
        /**
892
         * DOCUMENT ME!
893
         *
894
         * @param language
895
         * @param country
896
         * @param variant
897
         *
898
         * @return DOCUMENT ME!
899
         */
900
        private static Locale getLocale(String language, String country,
901
                String variant) {
902
                if (variant != null) {
903
                        return new Locale(language, country, variant);
904
                } else if (country != null) {
905
                        return new Locale(language, country);
906
                } else if (language != null) {
907
                        return new Locale(language);
908
                } else {
909
                        return new Locale("es");
910
                }
911
        }
912

    
913
        /**
914
         * DOCUMENT ME!
915
         *
916
         * @param file DOCUMENT ME!
917
         *
918
         * @throws IOException DOCUMENT ME!
919
         * @throws MarshalException DOCUMENT ME!
920
         * @throws ValidationException DOCUMENT ME!
921
         */
922
        private static void andamiConfigToXML(String file)
923
                throws IOException, MarshalException, ValidationException {
924
                File xml = new File(file);
925
                File parent = xml.getParentFile();
926
                parent.mkdirs();
927

    
928
                FileWriter writer = new FileWriter(xml);
929
                andamiConfig.marshal(writer);
930
        }
931

    
932
        /**
933
         * DOCUMENT ME!
934
         *
935
         * @param file DOCUMENT ME!
936
         *
937
         * @throws ConfigurationException DOCUMENT ME!
938
         */
939
        private static void andamiConfigFromXML(String file)
940
                throws ConfigurationException {
941
                File xml = new File(file);
942

    
943
                //Si no existe se ponen los valores por defecto
944
                if (!xml.exists()) {
945
                        andamiConfig = new AndamiConfig();
946

    
947
                        Andami andami = new Andami();
948
                        andami.setUpdate(true);
949
                        andamiConfig.setAndami(andami);
950
                        andamiConfig.setLocaleCountry(Locale.getDefault().getCountry());
951
                        andamiConfig.setLocaleLanguage(Locale.getDefault().getLanguage());
952
                        andamiConfig.setLocaleVariant(Locale.getDefault().getVariant());
953

    
954
                        if (System.getProperty("javawebstart.version") != null) // Es java web start)
955
                         {
956
                                andamiConfig.setPluginsDirectory(new File(System.getProperty(
957
                                                        "user.home") + File.separator + appName +
958
                                                File.separator + "extensiones").getAbsolutePath());
959
                        } else {
960
                                andamiConfig.setPluginsDirectory(new File(appName +
961
                                                File.separator + "extensiones").getAbsolutePath());
962
                        }
963

    
964
                        andamiConfig.setPlugin(new Plugin[0]);
965
                } else {
966
                        //Se lee la configuraci?n
967
                        FileReader reader;
968

    
969
                        try {
970
                                reader = new FileReader(xml);
971
                                andamiConfig = (AndamiConfig) AndamiConfig.unmarshal(reader);
972
                        } catch (FileNotFoundException e) {
973
                                throw new ConfigurationException(e);
974
                        } catch (MarshalException e) {
975
                                throw new ConfigurationException(e);
976
                        } catch (ValidationException e) {
977
                                throw new ConfigurationException(e);
978
                        }
979
                }
980
        }
981

    
982
        /**
983
         * DOCUMENT ME!
984
         *
985
         * @return DOCUMENT ME!
986
         *
987
         * @throws ConfigurationException DOCUMENT ME!
988
         */
989
        private static XMLEntity persistenceFromXML() throws ConfigurationException {
990
                File xml = new File(pluginsPersistencePath);
991

    
992
                if (xml.exists()) {
993
                        FileReader reader;
994

    
995
                        try {
996
                                reader = new FileReader(xml);
997

    
998
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
999

    
1000
                                return new XMLEntity(tag);
1001
                        } catch (FileNotFoundException e) {
1002
                                throw new ConfigurationException(e);
1003
                        } catch (MarshalException e) {
1004
                                throw new ConfigurationException(e);
1005
                        } catch (ValidationException e) {
1006
                                throw new ConfigurationException(e);
1007
                        }
1008
                } else {
1009
                        return new XMLEntity();
1010
                }
1011
        }
1012

    
1013
        /**
1014
         * DOCUMENT ME!
1015
         *
1016
         * @param entity DOCUMENT ME!
1017
         *
1018
         * @throws ConfigurationException DOCUMENT ME!
1019
         */
1020
        private static void persistenceToXML(XMLEntity entity)
1021
                throws ConfigurationException {
1022
                File xml = new File(pluginsPersistencePath);
1023

    
1024
                FileWriter writer;
1025

    
1026
                try {
1027
                        writer = new FileWriter(xml);
1028
                        entity.getXmlTag().marshal(writer);
1029
                } catch (FileNotFoundException e) {
1030
                        throw new ConfigurationException(e);
1031
                } catch (MarshalException e) {
1032
                        throw new ConfigurationException(e);
1033
                } catch (ValidationException e) {
1034
                        throw new ConfigurationException(e);
1035
                } catch (IOException e) {
1036
                        throw new ConfigurationException(e);
1037
                }
1038
        }
1039

    
1040
        /**
1041
         * Devuelve un array con los directorios de los plugins
1042
         *
1043
         * @param dirExt Directorio de las extensiones a partir del cual cuelgan
1044
         *                   todos los directorios de los plugins
1045
         *
1046
         * @return ArrayList con los directorios
1047
         */
1048
        private String[] getLocales(File dirExt) {
1049
                ArrayList types = new ArrayList();
1050
                File[] files = dirExt.listFiles();
1051

    
1052
                for (int i = 0; i < files.length; i++) {
1053
                        if (files[i].isDirectory()) {
1054
                                File[] textFile = files[i].listFiles(new FilenameFilter() {
1055
                                                        public boolean accept(File dir, String fileName) {
1056
                                                                return fileName.toLowerCase().startsWith("text_"); //$NON-NLS-1$
1057
                                                        }
1058
                                                });
1059

    
1060
                                for (int j = 0; j < textFile.length; j++) {
1061
                                        String s = (textFile[j]).getName().replaceAll("text_", "");
1062
                                        s = s.replaceAll(".properties", "");
1063
                                        s = s.trim();
1064

    
1065
                                        if (!types.contains(s)) {
1066
                                                types.add(s);
1067
                                        }
1068
                                }
1069
                        }
1070
                }
1071

    
1072
                return (String[]) types.toArray(new String[0]);
1073
        }
1074

    
1075
        /**
1076
         * DOCUMENT ME!
1077
         *
1078
         * @return Returns the frame.
1079
         */
1080
        static MDIFrame getFrame() {
1081
                return frame;
1082
        }
1083

    
1084
        /**
1085
         * Secuencia de cerrado de Andami
1086
         */
1087
        public static void closeApplication() {
1088
                //Configuraci?n de Andami
1089
                try {
1090
                        andamiConfigToXML(andamiConfigPath);
1091
                } catch (MarshalException e) {
1092
                        logger.error(Messages.getString(
1093
                                        "Launcher.No_se_pudo_guardar_la_configuracion_de_andami"), e);
1094
                } catch (ValidationException e) {
1095
                        logger.error(Messages.getString(
1096
                                        "Launcher.No_se_pudo_guardar_la_configuracion_de_andami"), e);
1097
                } catch (IOException e) {
1098
                        logger.error(Messages.getString(
1099
                                        "Launcher.No_se_pudo_guardar_la_configuracion_de_andami"), e);
1100
                }
1101

    
1102
                //Persistencia de los plugins
1103
                Iterator i = pluginsConfig.keySet().iterator();
1104

    
1105
                XMLEntity entity = new XMLEntity();
1106

    
1107
                while (i.hasNext()) {
1108
                        String pName = (String) i.next();
1109
                        PluginServices ps = (PluginServices) pluginsServices.get(pName);
1110
                        XMLEntity ent = ps.getPersistentXML();
1111

    
1112
                        if (ent != null) {
1113
                                ent.putProperty("com.iver.andami.pluginName", pName);
1114
                                entity.addChild(ent);
1115
                        }
1116
                }
1117

    
1118
                try {
1119
                        persistenceToXML(entity);
1120
                } catch (ConfigurationException e1) {
1121
                        logger.error(Messages.getString(
1122
                                        "Launcher.Se_produjo_un_error_guardando_la_configuracion_de_los_plugins"),
1123
                                e1);
1124
                }
1125

    
1126
                //Para la depuraci?n de memory leaks
1127
                System.gc();
1128

    
1129
        System.exit(0);
1130
        }
1131

    
1132
        /**
1133
         * DOCUMENT ME!
1134
         *
1135
         * @return DOCUMENT ME!
1136
         */
1137
        static HashMap getClassesExtensions() {
1138
                return classesExtensions;
1139
        }
1140

    
1141
        /**
1142
         * DOCUMENT ME!
1143
         *
1144
         * @param extDir DOCUMENT ME!
1145
         */
1146
        private static void downloadExtensions(String extDir) {
1147
                java.util.Date fechaActual = null;
1148

    
1149
                try {
1150
                        if (System.getProperty("javawebstart.version") != null) {
1151
                                //Obtenemos la URL del servidor
1152
                                BasicService bs = (BasicService) ServiceManager.lookup(
1153
                                                "javax.jnlp.BasicService");
1154
                                URL baseURL = bs.getCodeBase();
1155

    
1156
                                //Se descargan las extensiones
1157
                                SplashWindow.process(5,
1158
                                        "Descargando las extensiones desde " + baseURL + " a " +
1159
                                        extDir);
1160

    
1161
                                URL url = new URL(baseURL + "extensiones.zip");
1162
                                URLConnection connection = url.openConnection();
1163

    
1164
                                System.out.println(url.toExternalForm() + ":");
1165
                                System.out.println("  Content Type: " +
1166
                                        connection.getContentType());
1167
                                System.out.println("  Content Length: " +
1168
                                        connection.getContentLength());
1169
                                System.out.println("  Last Modified: " +
1170
                                        new Date(connection.getLastModified()));
1171
                                System.out.println("  Expiration: " +
1172
                                        connection.getExpiration());
1173
                                System.out.println("  Content Encoding: " +
1174
                                        connection.getContentEncoding());
1175

    
1176
                                // Guardamos la fecha del fichero de extensiones que nos hemos bajado, y
1177
                                // comprobamos el ?ltimo que se ha bajado. Si no son
1178
                                // iguales, nos bajamos el nuevo. Si son iguales, no
1179
                                // nos bajamos nada.
1180
                                Long miliSecondsInWeb = new Long(connection.getLastModified());
1181

    
1182
                                // PluginServices ps = PluginServices.getPluginServices("com.iver.core");
1183
                                // if (ps.getPersistentXML().getStringProperty("timestamp") != null)
1184
                                File destDir = new File(extDir);
1185

    
1186
                                if (!destDir.exists()) {
1187
                                        // Creamos gvSIG
1188
                                        destDir.getParentFile().mkdir();
1189

    
1190
                                        if (!destDir.mkdir()) {
1191
                                                System.err.println("Imposible crear el directorio " +
1192
                                                        destDir.getAbsolutePath());
1193
                                        }
1194
                                }
1195

    
1196
                                File timeFile = new File(destDir.getParent() + File.separator +
1197
                                                "timeStamp.properties");
1198

    
1199
                                if (!timeFile.exists()) {
1200
                                        timeFile.createNewFile();
1201
                                }
1202

    
1203
                                FileInputStream inAux = new FileInputStream(timeFile);
1204
                                Properties prop = new Properties();
1205
                                prop.load(inAux);
1206
                                inAux.close();
1207

    
1208
                                if (prop.getProperty("timestamp") != null) {
1209
                                        Long lastMiliSeconds = (Long) new Long(prop.getProperty(
1210
                                                                "timestamp"));
1211

    
1212
                                        if (lastMiliSeconds.longValue() == miliSecondsInWeb.longValue()) {
1213
                                                System.out.println("No hay nueva actualizaci?n");
1214

    
1215
                                                return;
1216
                                        }
1217

    
1218
                                        System.out.println("timeStampWeb= " + miliSecondsInWeb);
1219
                                        System.out.println("timeStampLocal= " + lastMiliSeconds);
1220
                                } else {
1221
                                        System.out.println("El timeStamp no est? escrito en " +
1222
                                                timeFile.getAbsolutePath());
1223
                                }
1224

    
1225
                                InputStream stream = connection.getInputStream();
1226
                                File temp = File.createTempFile("gvsig", ".zip");
1227
                                temp.deleteOnExit();
1228

    
1229
                                FileOutputStream file = new FileOutputStream(temp);
1230
                                BufferedInputStream in = new BufferedInputStream(stream);
1231
                                BufferedOutputStream out = new BufferedOutputStream(file);
1232

    
1233
                                int i;
1234
                                int pct;
1235
                                int desde;
1236
                                int hasta;
1237

    
1238
                                hasta = connection.getContentLength() / 1024;
1239
                                desde = 0;
1240

    
1241
                                while ((i = in.read()) != -1) {
1242
                                        pct = ((desde / 1024) * 100) / hasta;
1243

    
1244
                                        if (((desde % 10240) == 0) && (pct > 10) &&
1245
                                                        ((pct % 10) == 0)) {
1246
                                                SplashWindow.process(pct,
1247
                                                        (desde / 1024) + "Kb de " + hasta +
1248
                                                        "Kb descargados...");
1249
                                        }
1250

    
1251
                                        out.write(i);
1252
                                        desde++;
1253
                                }
1254

    
1255
                                out.flush();
1256
                                out.close();
1257
                                in.close();
1258

    
1259
                                //Se extrae el zip
1260
                                SplashWindow.process(5, "Extensiones descargadas.");
1261

    
1262
                                System.out.println("Extrayendo a " + destDir.getAbsolutePath());
1263

    
1264
                                Date fechaDir = new Date(destDir.lastModified());
1265
                                System.out.println("Fecha del directorio " + extDir + " = " +
1266
                                        fechaDir.toString());
1267
                                Utilities.extractTo(temp, new File(extDir), splashWindow);
1268

    
1269
                                // Si todo ha ido bien, guardamos el timestamp.
1270
                                ///  App.instance.getPc().addProperties("timestamp", miliSecondsInWeb);
1271
                                // XMLEntity xml=ps.getPersistentXML();
1272
                                fechaActual = new java.util.Date();
1273

    
1274
                                FileOutputStream outAux = new FileOutputStream(timeFile);
1275
                                prop.setProperty("timestamp", miliSecondsInWeb.toString());
1276
                                prop.store(outAux, "last download");
1277
                                outAux.close();
1278
                                System.out.println("Fecha actual guardada: " +
1279
                                        fechaActual.toGMTString());
1280

    
1281
                                /* xml.putProperty("timestamp",fechaActual.toGMTString());
1282
                                   ps.setPresistentXML(xml); */
1283
                        }
1284
                } catch (IOException e) {
1285
                        NotificationManager.addError("", e);
1286
                } catch (UnavailableServiceException e) {
1287
                        NotificationManager.addError("", e);
1288
                } catch (SecurityException e) {
1289
                        System.err.println("No se puede escribir el timeStamp " +
1290
                                fechaActual.toGMTString());
1291
                        NotificationManager.addError("", e);
1292
                }
1293
        }
1294

    
1295
        /**
1296
         * DOCUMENT ME!
1297
         *
1298
         * @return DOCUMENT ME!
1299
         */
1300
        private static Extensions[] getExtensions() {
1301
                ArrayList array = new ArrayList();
1302
                Iterator iter = pluginsConfig.values().iterator();
1303

    
1304
                while (iter.hasNext()) {
1305
                        array.add(((PluginConfig) iter.next()).getExtensions());
1306
                }
1307

    
1308
                return (Extensions[]) array.toArray(new Extensions[0]);
1309
        }
1310

    
1311
        /**
1312
         * DOCUMENT ME!
1313
         *
1314
         * @return DOCUMENT ME!
1315
         */
1316
        public static HashMap getPluginConfig() {
1317
                return pluginsConfig;
1318
        }
1319

    
1320
        /**
1321
         * DOCUMENT ME!
1322
         *
1323
         * @param s DOCUMENT ME!
1324
         *
1325
         * @return DOCUMENT ME!
1326
         */
1327
        public static Extension getExtension(String s) {
1328
                Extensions[] exts = getExtensions();
1329

    
1330
                for (int i = 0; i < exts.length; i++) {
1331
                        for (int j = 0; j < exts[i].getExtensionCount(); j++) {
1332
                                if (exts[i].getExtension(j).getClassName().equals(s)) {
1333
                                        return exts[i].getExtension(j);
1334
                                }
1335
                        }
1336
                }
1337

    
1338
                return null;
1339
        }
1340

    
1341
        /**
1342
         * DOCUMENT ME!
1343
         *
1344
         * @return DOCUMENT ME!
1345
         */
1346
        public static AndamiConfig getAndamiConfig() {
1347
                return andamiConfig;
1348
        }
1349

    
1350
        /**
1351
         * DOCUMENT ME!
1352
         *
1353
         * @author $author$
1354
         * @version $Revision: 1381 $
1355
         */
1356
        private static class ExtensionComparator implements Comparator {
1357
                /**
1358
                 * DOCUMENT ME!
1359
                 *
1360
                 * @param o1 DOCUMENT ME!
1361
                 * @param o2 DOCUMENT ME!
1362
                 *
1363
                 * @return DOCUMENT ME!
1364
                 */
1365
                public int compare(Object o1, Object o2) {
1366
                        Extension e1 = (Extension) o1;
1367
                        Extension e2 = (Extension) o2;
1368

    
1369
                        if (!e1.hasPriority() && !e2.hasPriority()) {
1370
                                return -1;
1371
                        }
1372

    
1373
                        if (e1.hasPriority() && !e2.hasPriority()) {
1374
                                return -Integer.MAX_VALUE;
1375
                        }
1376

    
1377
                        if (e2.hasPriority() && !e1.hasPriority()) {
1378
                                return Integer.MAX_VALUE;
1379
                        }
1380

    
1381
                        return e1.getPriority() - e2.getPriority();
1382
                }
1383
        }
1384

    
1385
        /**
1386
         * DOCUMENT ME!
1387
         */
1388
        private static class MenuComparator implements Comparator {
1389
                private static ExtensionComparator extComp = new ExtensionComparator();
1390

    
1391
                /**
1392
                 * DOCUMENT ME!
1393
                 *
1394
                 * @param o1 DOCUMENT ME!
1395
                 * @param o2 DOCUMENT ME!
1396
                 *
1397
                 * @return DOCUMENT ME!
1398
                 */
1399
                public int compare(Object o1, Object o2) {
1400
                        SortableMenu e1 = (SortableMenu) o1;
1401
                        SortableMenu e2 = (SortableMenu) o2;
1402

    
1403
                        if (!e1.menu.hasPosition() && !e2.menu.hasPosition()) {
1404
                                if (e1.extension instanceof SkinExtensionType) {
1405
                                        return 1;
1406
                                } else if (e2.extension instanceof SkinExtensionType) {
1407
                                        return -1;
1408
                                } else {
1409
                                        return extComp.compare(e1.extension, e2.extension);
1410
                                }
1411
                        }
1412

    
1413
                        if (e1.menu.hasPosition() && !e2.menu.hasPosition()) {
1414
                                return -Integer.MAX_VALUE;
1415
                        }
1416

    
1417
                        if (e2.menu.hasPosition() && !e1.menu.hasPosition()) {
1418
                                return Integer.MAX_VALUE;
1419
                        }
1420

    
1421
                        return e1.menu.getPosition() - e2.menu.getPosition();
1422
                }
1423
        }
1424

    
1425
        /**
1426
         * DOCUMENT ME!
1427
         *
1428
         * @author $author$
1429
         * @version $Revision: 1381 $
1430
         */
1431
        private static class SortableMenu {
1432
                public PluginClassLoader loader;
1433
                public Menu menu;
1434
                public SkinExtensionType extension;
1435

    
1436
                /**
1437
                 * DOCUMENT ME!
1438
                 *
1439
                 * @param loader DOCUMENT ME!
1440
                 * @param skinExt
1441
                 * @param menu2
1442
                 */
1443
                public SortableMenu(PluginClassLoader loader,
1444
                        SkinExtensionType skinExt, Menu menu2) {
1445
                        extension = skinExt;
1446
                        menu = menu2;
1447
                        this.loader = loader;
1448
                }
1449
        }
1450
}