KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > AdminClient


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.admin;
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 import com.tc.util.runtime.Os;
14
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.prefs.Preferences JavaDoc;
26
27 import javax.swing.Timer JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29
30 public class AdminClient extends ApplicationManager {
31   private static AdminClient m_client;
32   private AdminClientContext m_cntx;
33
34   private static final String JavaDoc PREF_FILE = ".AdminClient.xml";
35
36   static {
37     Logger.getLogger("javax.management.remote.generic").setLevel(Level.OFF);
38     Logger.getLogger("javax.management.remote.misc").setLevel(Level.OFF);
39     Logger.getLogger("com.sun.jmx.remote.opt.util").setLevel(Level.OFF);
40     Logger.getLogger("com.sun.jmx.remote.opt.util").setLevel(Level.OFF);
41     Logger.getLogger("javax.management.remote.rmi").setLevel(Level.OFF);
42   }
43   
44   protected AdminClient() {
45     super("AdminClient");
46
47     if(Os.isMac()) {
48       System.setProperty("com.apple.macos.useScreenMenuBar", "true");
49       System.setProperty("apple.laf.useScreenMenuBar", "true");
50
51       System.setProperty("apple.awt.showGrowBox", "true");
52       System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
53     }
54
55     m_cntx = new AdminClientContext();
56     m_cntx.client = m_client = this;
57     m_cntx.prefs = loadPrefs();
58     m_cntx.topRes = loadTopRes();
59     m_cntx.bundleHelper = new ResourceBundleHelper(getClass());
60
61     if(!Boolean.getBoolean("com.tc.ui.java-icon")) {
62       setIconImage(new Image(getBytes("/com/tc/admin/icons/logo_small.gif")));
63     }
64   }
65
66   static byte[] getBytes(String JavaDoc path) {
67     byte[] result = null;
68     URL JavaDoc url = AdminClient.class.getResource(path);
69     
70     if(url != null) {
71       InputStream JavaDoc is = null;
72       
73       try {
74         result = IOUtils.toByteArray(is = url.openStream());
75       } catch(IOException JavaDoc ioe) {
76         ioe.printStackTrace();
77       } finally {
78         IOUtils.closeQuietly(is);
79       }
80     }
81     
82     return result;
83   }
84
85   public static AdminClient getClient() {
86     if(m_client == null) {
87       new AdminClient().parseArgs(new String JavaDoc[]{});
88     }
89
90     return m_client;
91   }
92
93   protected AdminClientContext context() {
94     return m_cntx;
95   }
96
97   public static AdminClientContext getContext() {
98     return getClient().context();
99   }
100
101   /**
102    * We use java.util.prefs instead of Dijon resources.
103    */

104   public DictionaryResource loadPreferences() {
105     return new DictionaryResource();
106   }
107   public void storePreferences() {/**/}
108
109   private Preferences JavaDoc loadPrefs() {
110     FileInputStream JavaDoc fis = null;
111
112     try {
113       File JavaDoc f = new File JavaDoc(System.getProperty("user.home"), PREF_FILE);
114
115       if(f.exists()) {
116         fis = new FileInputStream JavaDoc(f);
117         Preferences.importPreferences(fis);
118       }
119     } catch(Exception JavaDoc e) {
120       // ignore
121
} finally {
122       IOUtils.closeQuietly(fis);
123     }
124
125     return Preferences.userNodeForPackage(getClass());
126   }
127
128   public void storePrefs() {
129     FileOutputStream JavaDoc fos = null;
130
131     try {
132       File JavaDoc f = new File JavaDoc(System.getProperty("user.home"), PREF_FILE);
133       fos = new FileOutputStream JavaDoc(f);
134       m_cntx.prefs.exportSubtree(fos);
135       m_cntx.prefs.flush();
136     } catch(Exception JavaDoc e) {
137       e.printStackTrace();
138     } finally {
139       IOUtils.closeQuietly(fos);
140     }
141   }
142
143   private DictionaryResource loadTopRes() {
144     DictionaryResource topRes = null;
145     InputStream JavaDoc is = null;
146
147     try {
148       is = getClass().getResourceAsStream("AdminClient.xml");
149       topRes = ApplicationManager.loadResource(is);
150     } catch(Throwable JavaDoc t) {
151       t.printStackTrace();
152       System.exit(-1);
153     } finally {
154       IOUtils.closeQuietly(is);
155     }
156
157     return topRes;
158   }
159
160   public void start() {
161     m_cntx.controller = new AdminClientFrame();
162     Timer JavaDoc t = new Timer JavaDoc(2000, new ActionListener JavaDoc() {
163       public void actionPerformed(ActionEvent JavaDoc ae) {
164         ((AdminClientFrame)m_cntx.controller).setVisible(true);
165         splashProc.destroy();
166       }
167     });
168     t.setRepeats(false);
169     t.start();
170   }
171
172   public String JavaDoc[] parseArgs(String JavaDoc[] args) {
173     args = super.parseArgs(args);
174
175     if (args != null && args.length > 0) {
176       // There may be arguments in the future
177
}
178
179     return args;
180   }
181
182   private static Process JavaDoc splashProc;
183
184   public static final void main(final String JavaDoc[] args)
185     throws Exception JavaDoc
186   {
187     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
188     
189     splashProc = Splash.start("Starting Terracotta AdminConsole...", new Runnable JavaDoc() {
190       public void run() {
191         AdminClient client = new AdminClient();
192         client.parseArgs(ApplicationManager.parseLAFArgs(args));
193         client.start();
194       }
195     });
196     splashProc.waitFor();
197   }
198 }
199
Popular Tags