KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > SessionIntegrator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc;
5
6 import org.apache.commons.io.IOUtils;
7 import org.dijon.ApplicationManager;
8 import org.dijon.DictionaryResource;
9 import org.dijon.Image;
10
11 import com.tc.admin.common.Splash;
12 import com.tc.util.ResourceBundleHelper;
13
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.prefs.Preferences JavaDoc;
23
24 import javax.swing.Timer JavaDoc;
25 import javax.swing.UIManager JavaDoc;
26
27 public class SessionIntegrator extends ApplicationManager {
28   private static SessionIntegrator m_client;
29   private SessionIntegratorContext m_cntx;
30
31   private static final String JavaDoc APP_NAME = "SessionIntegrator";
32   private static final String JavaDoc PREF_FILE = "."+APP_NAME+".xml";
33   
34   public SessionIntegrator() {
35     super(APP_NAME);
36     
37     m_cntx = new SessionIntegratorContext();
38     m_cntx.client = m_client = this;
39     m_cntx.prefs = loadPrefs();
40     m_cntx.topRes = loadTopRes();
41     m_cntx.bundleHelper = new ResourceBundleHelper(getClass());
42
43     setIconImage(new Image(getBytes("/com/tc/admin/icons/logo_small.gif")));
44   }
45   
46   static byte[] getBytes(String JavaDoc path) {
47     byte[] result = null;
48     URL JavaDoc url = SessionIntegrator.class.getResource(path);
49     
50     if(url != null) {
51       InputStream JavaDoc is = null;
52       
53       try {
54         result = IOUtils.toByteArray(is = url.openStream());
55       } catch(IOException JavaDoc ioe) {
56         ioe.printStackTrace();
57       } finally {
58         IOUtils.closeQuietly(is);
59       }
60     }
61     
62     return result;
63   }
64   
65   public void toConsole(String JavaDoc msg) {
66     /**/
67   }
68   
69   public static SessionIntegrator getClient() {
70     return m_client;
71   }
72
73   protected SessionIntegratorContext context() {
74     return m_cntx;
75   }
76
77   public static SessionIntegratorContext getContext() {
78     return getClient().context();
79   }
80
81   public DictionaryResource loadPreferences() {
82     return new DictionaryResource();
83   }
84   
85   public void storePreferences() {/**/}
86
87   private Preferences JavaDoc loadPrefs() {
88     FileInputStream JavaDoc fis = null;
89
90     try {
91       File JavaDoc f = new File JavaDoc(System.getProperty("user.home"), PREF_FILE);
92
93       if(f.exists()) {
94         fis = new FileInputStream JavaDoc(f);
95         Preferences.importPreferences(fis);
96       }
97     } catch(Exception JavaDoc e) {
98       // ignore
99
} finally {
100       IOUtils.closeQuietly(fis);
101     }
102
103     return Preferences.userNodeForPackage(getClass());
104   }
105
106   public void storePrefs() {
107     FileOutputStream JavaDoc fos = null;
108
109     try {
110       File JavaDoc f = new File JavaDoc(System.getProperty("user.home"), PREF_FILE);
111       fos = new FileOutputStream JavaDoc(f);
112       m_cntx.prefs.exportSubtree(fos);
113       m_cntx.prefs.flush();
114     } catch(Exception JavaDoc e) {
115       e.printStackTrace();
116     } finally {
117       IOUtils.closeQuietly(fos);
118     }
119   }
120
121   private DictionaryResource loadTopRes() {
122     DictionaryResource topRes = null;
123     InputStream JavaDoc is = null;
124
125     try {
126       is = getClass().getResourceAsStream(APP_NAME+".xml");
127       topRes = ApplicationManager.loadResource(is);
128     } catch(Throwable JavaDoc t) {
129       t.printStackTrace();
130       System.exit(-1);
131     } finally {
132       IOUtils.closeQuietly(is);
133     }
134
135     return topRes;
136   }
137
138   public void start() {
139     m_cntx.frame = new SessionIntegratorFrame();
140     Timer JavaDoc t = new Timer JavaDoc(1000, new ActionListener JavaDoc() {
141       public void actionPerformed(ActionEvent JavaDoc ae) {
142         m_cntx.frame.setVisible(true);
143         splashProc.destroy();
144       }
145     });
146     t.setRepeats(false);
147     t.start();
148   }
149
150   public String JavaDoc[] parseArgs(String JavaDoc[] args) {
151     args = super.parseArgs(args);
152
153     if (args != null && args.length > 0) {
154       // There may be arguments in the future
155
}
156
157     return args;
158   }
159
160   private static Process JavaDoc splashProc;
161
162   public static final void main(final String JavaDoc[] args)
163     throws Exception JavaDoc
164   {
165     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
166     
167     splashProc = Splash.start("Starting Terracotta Sessions Configurator...", new Runnable JavaDoc() {
168       public void run() {
169         SessionIntegrator client = new SessionIntegrator();
170         client.parseArgs(ApplicationManager.parseLAFArgs(args));
171         client.start();
172       }
173     });
174     splashProc.waitFor();
175   }
176 }
177
Popular Tags