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;
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.prefs.Preferences;
024:
025: import javax.swing.Timer;
026: import javax.swing.UIManager;
027:
028: public class SessionIntegrator extends ApplicationManager {
029: private static SessionIntegrator m_client;
030: private SessionIntegratorContext m_cntx;
031:
032: private static final String APP_NAME = "SessionIntegrator";
033: private static final String PREF_FILE = "." + APP_NAME + ".xml";
034:
035: public SessionIntegrator() {
036: super (APP_NAME);
037:
038: if (Os.isMac()) {
039: System.setProperty("com.apple.macos.useScreenMenuBar",
040: "true");
041: System.setProperty("apple.laf.useScreenMenuBar", "true");
042: System.setProperty("apple.awt.showGrowBox", "true");
043: System.setProperty(
044: "com.apple.mrj.application.growbox.intrudes",
045: "false");
046: }
047:
048: m_cntx = new SessionIntegratorContext();
049: m_cntx.client = m_client = this ;
050: m_cntx.prefs = loadPrefs();
051: m_cntx.topRes = loadTopRes();
052: m_cntx.bundleHelper = new ResourceBundleHelper(getClass());
053:
054: setIconImage(new Image(
055: getBytes("/com/tc/admin/icons/logo_small.gif")));
056: }
057:
058: static byte[] getBytes(String path) {
059: byte[] result = null;
060: URL url = SessionIntegrator.class.getResource(path);
061:
062: if (url != null) {
063: InputStream is = null;
064:
065: try {
066: result = IOUtils.toByteArray(is = url.openStream());
067: } catch (IOException ioe) {
068: ioe.printStackTrace();
069: } finally {
070: IOUtils.closeQuietly(is);
071: }
072: }
073:
074: return result;
075: }
076:
077: public void toConsole(String msg) {
078: /**/
079: }
080:
081: public static SessionIntegrator getClient() {
082: return m_client;
083: }
084:
085: protected SessionIntegratorContext context() {
086: return m_cntx;
087: }
088:
089: public static SessionIntegratorContext getContext() {
090: return getClient().context();
091: }
092:
093: public DictionaryResource loadPreferences() {
094: return new DictionaryResource();
095: }
096:
097: public void storePreferences() {/**/
098: }
099:
100: private Preferences loadPrefs() {
101: FileInputStream fis = null;
102:
103: try {
104: File f = new File(System.getProperty("user.home"),
105: PREF_FILE);
106:
107: if (f.exists()) {
108: fis = new FileInputStream(f);
109: Preferences.importPreferences(fis);
110: }
111: } catch (Exception e) {
112: // ignore
113: } finally {
114: IOUtils.closeQuietly(fis);
115: }
116:
117: return Preferences.userNodeForPackage(getClass());
118: }
119:
120: public void storePrefs() {
121: FileOutputStream fos = null;
122:
123: try {
124: File f = new File(System.getProperty("user.home"),
125: PREF_FILE);
126: fos = new FileOutputStream(f);
127: m_cntx.prefs.exportSubtree(fos);
128: m_cntx.prefs.flush();
129: } catch (Exception e) {
130: e.printStackTrace();
131: } finally {
132: IOUtils.closeQuietly(fos);
133: }
134: }
135:
136: private DictionaryResource loadTopRes() {
137: DictionaryResource topRes = null;
138: InputStream is = null;
139:
140: try {
141: is = getClass().getResourceAsStream(APP_NAME + ".xml");
142: topRes = ApplicationManager.loadResource(is);
143: } catch (Throwable t) {
144: t.printStackTrace();
145: System.exit(-1);
146: } finally {
147: IOUtils.closeQuietly(is);
148: }
149:
150: return topRes;
151: }
152:
153: public void start() {
154: m_cntx.frame = new SessionIntegratorFrame();
155: Timer t = new Timer(1000, new ActionListener() {
156: public void actionPerformed(ActionEvent ae) {
157: m_cntx.frame.setVisible(true);
158: splashProc.destroy();
159: }
160: });
161: t.setRepeats(false);
162: t.start();
163: }
164:
165: public String[] parseArgs(String[] args) {
166: args = super .parseArgs(args);
167:
168: if (args != null && args.length > 0) {
169: // There may be arguments in the future
170: }
171:
172: return args;
173: }
174:
175: private static Process splashProc;
176:
177: public static final void main(final String[] args) throws Exception {
178: UIManager.setLookAndFeel(UIManager
179: .getSystemLookAndFeelClassName());
180:
181: splashProc = Splash.start(
182: "Starting Terracotta Sessions Configurator...",
183: new Runnable() {
184: public void run() {
185: SessionIntegrator client = new SessionIntegrator();
186: client.parseArgs(ApplicationManager
187: .parseLAFArgs(args));
188: client.start();
189: }
190: });
191: splashProc.waitFor();
192: }
193: }
|