001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin;
005:
006: import org.apache.commons.io.IOUtils;
007: import org.dijon.ApplicationManager;
008: import org.dijon.DictionaryResource;
009: import org.dijon.Image;
010:
011: import com.tc.admin.common.Splash;
012: import com.tc.util.ResourceBundleHelper;
013: import com.tc.util.runtime.Os;
014:
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import java.util.prefs.Preferences;
026:
027: import javax.swing.Timer;
028: import javax.swing.UIManager;
029:
030: public class AdminClient extends ApplicationManager {
031: private static AdminClient m_client;
032: private AdminClientContext m_cntx;
033:
034: private static final String PREF_FILE = ".AdminClient.xml";
035:
036: static {
037: Logger.getLogger("javax.management.remote.generic").setLevel(
038: Level.OFF);
039: Logger.getLogger("javax.management.remote.misc").setLevel(
040: Level.OFF);
041: Logger.getLogger("com.sun.jmx.remote.opt.util").setLevel(
042: Level.OFF);
043: Logger.getLogger("com.sun.jmx.remote.opt.util").setLevel(
044: Level.OFF);
045: Logger.getLogger("javax.management.remote.rmi").setLevel(
046: Level.OFF);
047: }
048:
049: protected AdminClient() {
050: super ("AdminClient");
051:
052: if (Os.isMac()) {
053: System.setProperty("com.apple.macos.useScreenMenuBar",
054: "true");
055: System.setProperty("apple.laf.useScreenMenuBar", "true");
056:
057: System.setProperty("apple.awt.showGrowBox", "true");
058: System.setProperty(
059: "com.apple.mrj.application.growbox.intrudes",
060: "false");
061: }
062:
063: m_cntx = new AdminClientContext();
064: m_cntx.client = m_client = this ;
065: m_cntx.prefs = loadPrefs();
066: m_cntx.topRes = loadTopRes();
067: m_cntx.bundleHelper = new ResourceBundleHelper(getClass());
068:
069: if (!Boolean.getBoolean("com.tc.ui.java-icon")) {
070: setIconImage(new Image(
071: getBytes("/com/tc/admin/icons/logo_small.gif")));
072: }
073: }
074:
075: static byte[] getBytes(String path) {
076: byte[] result = null;
077: URL url = AdminClient.class.getResource(path);
078:
079: if (url != null) {
080: InputStream is = null;
081:
082: try {
083: result = IOUtils.toByteArray(is = url.openStream());
084: } catch (IOException ioe) {
085: ioe.printStackTrace();
086: } finally {
087: IOUtils.closeQuietly(is);
088: }
089: }
090:
091: return result;
092: }
093:
094: public static AdminClient getClient() {
095: if (m_client == null) {
096: new AdminClient().parseArgs(new String[] {});
097: }
098:
099: return m_client;
100: }
101:
102: protected AdminClientContext context() {
103: return m_cntx;
104: }
105:
106: public static AdminClientContext getContext() {
107: return getClient().context();
108: }
109:
110: /**
111: * We use java.util.prefs instead of Dijon resources.
112: */
113: public DictionaryResource loadPreferences() {
114: return new DictionaryResource();
115: }
116:
117: public void storePreferences() {/**/
118: }
119:
120: private Preferences loadPrefs() {
121: FileInputStream fis = null;
122:
123: try {
124: File f = new File(System.getProperty("user.home"),
125: PREF_FILE);
126:
127: if (f.exists()) {
128: fis = new FileInputStream(f);
129: Preferences.importPreferences(fis);
130: }
131: } catch (Exception e) {
132: // ignore
133: } finally {
134: IOUtils.closeQuietly(fis);
135: }
136:
137: return Preferences.userNodeForPackage(getClass());
138: }
139:
140: public void storePrefs() {
141: FileOutputStream fos = null;
142:
143: try {
144: File f = new File(System.getProperty("user.home"),
145: PREF_FILE);
146: fos = new FileOutputStream(f);
147: m_cntx.prefs.exportSubtree(fos);
148: m_cntx.prefs.flush();
149: } catch (Exception e) {
150: e.printStackTrace();
151: } finally {
152: IOUtils.closeQuietly(fos);
153: }
154: }
155:
156: private DictionaryResource loadTopRes() {
157: DictionaryResource topRes = null;
158: InputStream is = null;
159:
160: try {
161: is = getClass().getResourceAsStream("AdminClient.xml");
162: topRes = ApplicationManager.loadResource(is);
163: } catch (Throwable t) {
164: t.printStackTrace();
165: System.exit(-1);
166: } finally {
167: IOUtils.closeQuietly(is);
168: }
169:
170: return topRes;
171: }
172:
173: public void start() {
174: m_cntx.controller = new AdminClientFrame();
175: Timer t = new Timer(2000, new ActionListener() {
176: public void actionPerformed(ActionEvent ae) {
177: ((AdminClientFrame) m_cntx.controller).setVisible(true);
178: splashProc.destroy();
179: }
180: });
181: t.setRepeats(false);
182: t.start();
183: }
184:
185: public String[] parseArgs(String[] args) {
186: args = super .parseArgs(args);
187:
188: if (args != null && args.length > 0) {
189: // There may be arguments in the future
190: }
191:
192: return args;
193: }
194:
195: private static Process splashProc;
196:
197: public static final void main(final String[] args) throws Exception {
198: UIManager.setLookAndFeel(UIManager
199: .getSystemLookAndFeelClassName());
200:
201: splashProc = Splash.start(
202: "Starting Terracotta AdminConsole...", new Runnable() {
203: public void run() {
204: AdminClient client = new AdminClient();
205: client.parseArgs(ApplicationManager
206: .parseLAFArgs(args));
207: client.start();
208: }
209: });
210: splashProc.waitFor();
211: }
212: }
|