KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > base > app > IpanemaApplication


1 package com.nightlabs.ipanema.base.app;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.LinkedList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.apache.log4j.PropertyConfigurator;
11 import org.eclipse.core.runtime.IPlatformRunnable;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.ui.PlatformUI;
14
15 import com.nightlabs.rcp.exceptionhandler.ExceptionHandlingThreadGroup;
16 import com.nightlabs.util.Utils;
17
18 /**
19  * IpanemaApplication is the main executed class {@see IpanemaApplication#run(Object)}.
20  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
21  */

22 public class IpanemaApplication
23 implements IPlatformRunnable
24 {
25     public static final String JavaDoc PLUGIN_ID = "com.nightlabs.ipanema.base"; //$NON-NLS-1$
26
public static final Logger LOGGER = Logger.getLogger(IpanemaApplication.class);
27
28     public IpanemaApplication()
29     {
30       
31     }
32     
33     
34     private static String JavaDoc rootDir = "";
35
36     /**
37      * Returns the root directory, which is ".ipanema" in the users home directory.
38      * @return
39      */

40     public static String JavaDoc getRootDir() {
41         if (rootDir.equals("")){
42             File JavaDoc rootFile = new File JavaDoc(System.getProperty("user.home"), ".ipanema");
43             rootFile.mkdirs();
44             rootDir = rootFile.getAbsolutePath();
45         }
46         return rootDir;
47     }
48     
49     private static String JavaDoc configDir = "";
50     /**
51      * Returns the config directory, which is getRootDir()+"/config".
52      * @return
53      */

54     public static String JavaDoc getConfigDir() {
55         if (configDir.equals("")){
56             File JavaDoc configFile = new File JavaDoc(getRootDir(),"config");
57             configFile.mkdirs();
58             configDir = configFile.getAbsolutePath();
59         }
60         return configDir;
61     }
62     
63     
64     private static String JavaDoc logDir = "";
65     
66     public static String JavaDoc getLogDir() {
67         if (logDir.equals("")){
68             File JavaDoc logFile = new File JavaDoc(getRootDir(),"log");
69             logFile.mkdirs();
70             logDir = logFile.getAbsolutePath();
71         }
72         return logDir;
73     }
74
75         private static Object JavaDoc mutex = new Object JavaDoc();
76         public static Object JavaDoc getMutex() {
77             return mutex;
78         }
79    
80     
81     
82     /**
83      * First initializes the {@link com.nightlabs.config.Config} in the users home
84      * directory under ".ipanema2/config". Sets static members of this class for
85      * other plugins to access the root of the ipanema2 folder and log.
86      * Creates a display in {@link org.eclipse.ui.PlatformUI} and a new
87      * {@link IpanemaWorkbenchAdvisor} and runs the Application.
88      * @see org.eclipse.core.boot.IPlatformRunnable#run(java.lang.Object)
89      */

90     public Object JavaDoc run(Object JavaDoc args)
91     throws Exception JavaDoc
92     {
93       try {
94 // initializeLoginModule();
95

96 // TODO set another SafeRunnable-Runner as soon as we updated Eclipse...
97
// SafeRunnable.setRunner(...)
98

99         ExceptionHandlingThreadGroup threadGroup = new ExceptionHandlingThreadGroup("IpanemaThreadGroup");
100         IpanemaApplicationThread ipThread = new IpanemaApplicationThread(threadGroup);
101         ipThread.setIpanemaApplication(this);
102         ipThread.start();
103         synchronized (mutex) {
104             mutex.wait();
105         }
106         
107             if (ipThread.getPlatformResultCode() == PlatformUI.RETURN_RESTART)
108                 return IPlatformRunnable.EXIT_RESTART;
109             else
110                 return IPlatformRunnable.EXIT_OK;
111         } finally {
112             if (Display.getCurrent() != null)
113                 Display.getCurrent().dispose();
114         }
115     }
116     
117     
118     /**
119      * Configures log4j with the file located in {@link #getConfigDir()}+"/log4j.properties"
120      * @throws IOException
121      */

122     public static void initializeLogging() throws IOException JavaDoc{
123     String JavaDoc logConfFileName = getConfigDir()+File.separatorChar+"log4j.properties";
124     File JavaDoc logProp = new File JavaDoc(logConfFileName);
125     if (!logProp.exists()){
126         // if not there copy
127
Utils.copyResource(IpanemaApplication.class,"log4j.properties",logConfFileName);
128     }
129     PropertyConfigurator.configure(logConfFileName);
130     LOGGER.info("Ipanema started.");
131     }
132     
133 // protected void initializeLoginModule(){
134
// LOGGER.debug("Declaring Configuration()");
135
// IpanemaSecurityConfiguration.declareConfiguration();
136
//
137
// LOGGER.debug("Setting login handler");
138
// try {
139
// Login.getLogin(false).setLoginHandler(new IpanemaLoginHandler());
140
// } catch (LoginException e) {
141
// throw new RuntimeException("How the hell could that happen!", e);
142
// }
143
// }
144

145     
146     private static List JavaDoc applicationListener = new LinkedList JavaDoc();
147     
148     public static void addApplicationListener(IpanemaApplicationListener listener) {
149         applicationListener.add(listener);
150     }
151     
152     public static void removeApplicationListener(IpanemaApplicationListener listener) {
153         applicationListener.remove(listener);
154     }
155     
156     public static final int APPLICATION_EVENTTYPE_STARTED = 1;
157     
158     void noitfyApplicationListeners(int applicationEventType) {
159         for (Iterator JavaDoc iter = applicationListener.iterator(); iter.hasNext();) {
160             IpanemaApplicationListener listener = (IpanemaApplicationListener) iter.next();
161             switch (applicationEventType) {
162                 case APPLICATION_EVENTTYPE_STARTED:
163                     listener.applicationStarted();
164                     break;
165             }
166         }
167     }
168     
169     
170 }
171
Popular Tags