1 package com.nightlabs.ipanema.base.app; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.Iterator ; 6 import java.util.LinkedList ; 7 import java.util.List ; 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 22 public class IpanemaApplication 23 implements IPlatformRunnable 24 { 25 public static final String PLUGIN_ID = "com.nightlabs.ipanema.base"; public static final Logger LOGGER = Logger.getLogger(IpanemaApplication.class); 27 28 public IpanemaApplication() 29 { 30 31 } 32 33 34 private static String rootDir = ""; 35 36 40 public static String getRootDir() { 41 if (rootDir.equals("")){ 42 File rootFile = new File (System.getProperty("user.home"), ".ipanema"); 43 rootFile.mkdirs(); 44 rootDir = rootFile.getAbsolutePath(); 45 } 46 return rootDir; 47 } 48 49 private static String configDir = ""; 50 54 public static String getConfigDir() { 55 if (configDir.equals("")){ 56 File configFile = new File (getRootDir(),"config"); 57 configFile.mkdirs(); 58 configDir = configFile.getAbsolutePath(); 59 } 60 return configDir; 61 } 62 63 64 private static String logDir = ""; 65 66 public static String getLogDir() { 67 if (logDir.equals("")){ 68 File logFile = new File (getRootDir(),"log"); 69 logFile.mkdirs(); 70 logDir = logFile.getAbsolutePath(); 71 } 72 return logDir; 73 } 74 75 private static Object mutex = new Object (); 76 public static Object getMutex() { 77 return mutex; 78 } 79 80 81 82 90 public Object run(Object args) 91 throws Exception 92 { 93 try { 94 96 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 122 public static void initializeLogging() throws IOException { 123 String logConfFileName = getConfigDir()+File.separatorChar+"log4j.properties"; 124 File logProp = new File (logConfFileName); 125 if (!logProp.exists()){ 126 Utils.copyResource(IpanemaApplication.class,"log4j.properties",logConfFileName); 128 } 129 PropertyConfigurator.configure(logConfFileName); 130 LOGGER.info("Ipanema started."); 131 } 132 133 145 146 private static List applicationListener = new LinkedList (); 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 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 |