Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: it/rabellino/toska/gui/KeysAdmin.java


1   package it.rabellino.toska.gui;
2   
3   import it.rabellino.toska.ConfigHandler;
4   import it.rabellino.toska.XMLConfigHandler;
5   import java.awt.BorderLayout;
6   import java.io.File;
7   import java.io.FileInputStream;
8   import java.io.InputStream;
9   
10  import javax.swing.ImageIcon;
11  import javax.swing.JFrame;
12  import javax.swing.JLabel;
13  import javax.swing.JOptionPane;
14  import javax.swing.JSplitPane;
15  import javax.swing.tree.DefaultMutableTreeNode;
16  
17  import org.apache.avalon.excalibur.component.DefaultRoleManager;
18  import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
19  import org.apache.avalon.excalibur.logger.LogKitLoggerManager;
20  
21  import org.apache.avalon.framework.activity.Initializable;
22  import org.apache.avalon.framework.component.Component;
23  import org.apache.avalon.framework.component.ComponentManager;
24  import org.apache.avalon.framework.component.Composable;
25  import org.apache.avalon.framework.configuration.Configurable;
26  import org.apache.avalon.framework.configuration.Configuration;
27  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
28  import org.apache.avalon.framework.context.Context;
29  import org.apache.avalon.framework.context.ContextException;
30  import org.apache.avalon.framework.context.Contextualizable;
31  import org.apache.avalon.framework.context.DefaultContext;
32  import org.apache.avalon.framework.logger.LogEnabled;
33  import org.apache.avalon.framework.logger.Logger;
34  
35  import org.apache.log.Hierarchy;
36  import org.apache.log.Priority;
37  import org.apache.log.format.PatternFormatter;
38  import org.apache.log.output.io.StreamTarget;
39  
40  
41  import org.xml.sax.InputSource;
42  
43  
44  /**
45   * This is the main class for the GUI version of Toska.
46   * 
47   * @author <a href="gianugo@apache.org">Gianugo Rabellino</a>
48   *
49   */
50  
51  public class KeysAdmin 
52    extends JFrame  
53    implements 
54      Component, 
55      Composable,
56      Configurable,
57      Contextualizable,
58      Initializable, 
59      LogEnabled {
60  
61  
62    public static final String APP_NAME = "Toska";
63    public static final String APP_FULLNAME = " Tool for OpenSsh Key Administration";
64    public static final String APP_VERSION = "0.5";
65    
66    protected Logger logger;
67    protected ExcaliburComponentManager manager;
68    protected Configuration config;
69    protected DefaultContext context;
70    
71    // Logger kludge. Avalon people, usque tandem?
72    
73    protected org.apache.log.Logger oldLogger;
74    
75    private ConfigHandler handler;
76    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Configuration");
77    private KeysTree keysTree;
78    private JSplitPane splitPane;
79    private TextAreaOutputStream taos;
80    
81    
82    public KeysAdmin() {
83          
84    }
85    
86  
87      
88    public void compose(ComponentManager manager) {
89      this.manager = (ExcaliburComponentManager)manager;  
90    }
91    
92    public void configure(Configuration config) {
93      this.config = config; 
94    }
95    
96  
97    
98    public void enableLogging(Logger logger) {
99      this.logger = logger;
100   
101   }
102   
103   public void open(File fileName) {
104     try {      
105        context.put(Constants.DATASTREAM,
106             new FileInputStream(fileName));
107       ((XMLConfigHandler)handler).marshal(); 
108       KeysMenuBar menuBar = (KeysMenuBar)context.get(Constants.MENUBAR);
109       menuBar.getSaveMenu().setEnabled(true);
110       menuBar.getDeployMenu().setEnabled(true);
111     } catch (Exception e) {
112       JOptionPane.showMessageDialog(this, "Problem opening " + 
113          fileName.getName() + ": " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
114       e.printStackTrace(System.err);
115       return;   
116     }
117           
118     keysTree.populateTree(handler);
119 
120   }  
121   
122   public ConfigHandler getConfigHandler() {
123     return handler;
124   }    
125 
126   public void setConfigHandler(ConfigHandler handler) {
127     if (this.handler.hasChanged()) {
128       int retval = JOptionPane.showConfirmDialog(null,
129         "Some data have been changed:\n" +
130         "Proceed anyway?"); 
131       if (retval != JOptionPane.YES_OPTION) return;
132     }
133     this.handler = handler;
134     context.put(Constants.CONFIGHANDLER, handler);
135     keysTree.populateTree(handler);
136   }
137   
138   public void populateTree() {
139     keysTree.populateTree(handler);
140   }  
141  
142 
143   public void save(File toFile) {
144     this.context.put(Constants.DATASINK, toFile);
145     ((XMLConfigHandler)handler).unmarshal();
146     logger.info("Succesfully saved: " + toFile.toString());
147   }
148   
149     /** Initialize the application */
150   public void initialize() {
151 
152     this.initLogger(); 
153 
154     DefaultRoleManager drm = new DefaultRoleManager();
155     try {
156       DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
157 
158       InputStream is = 
159           Thread.currentThread().getContextClassLoader().getResource("it/rabellino/toska/toska.roles").openStream();
160 
161       Configuration roleConfig = builder.build(is);
162       drm.setLogger(oldLogger);
163       //drm.enableLogging(logger);
164 
165       drm.configure(roleConfig);
166       
167       
168       ((ExcaliburComponentManager)manager).setLogger(oldLogger);
169       
170       manager.setRoleManager(drm);
171       
172       manager.configure(config);
173       manager.initialize();
174 
175   
176       handler = (ConfigHandler)manager.lookup(ConfigHandler.ROLE);
177       context.put(Constants.CONFIGHANDLER, handler);
178       // Main window
179       this.getContentPane().setLayout(new BorderLayout());
180       this.setTitle(APP_FULLNAME); 
181       ClassLoader cl = KeysAdmin.class.getClassLoader();    
182       this.setIconImage(new ImageIcon(cl.getResource("resources/toska16.jpg")).getImage());   
183       
184       // Log window
185       this.getContentPane().add(taos.getPane(), BorderLayout.SOUTH);
186       
187       // Menu bar
188       KeysMenuBar menuBar = (KeysMenuBar)manager.lookup(KeysMenuBar.ROLE);
189       this.getContentPane().add(menuBar, BorderLayout.NORTH);
190       menuBar.getSaveMenu().setEnabled(false);
191       menuBar.getDeployMenu().setEnabled(false);
192       menuBar.getDistributeMenu().setEnabled(false);
193        
194       
195       // Configuration tree
196       keysTree = (KeysTree)manager.lookup(KeysTree.ROLE);
197 
198       //Main split pane      
199       
200       splitPane = new JSplitPane();
201       
202       splitPane.setLeftComponent(keysTree);
203       splitPane.setRightComponent(new JLabel(new ImageIcon(cl.getResource("resources/toska.jpg"))));
204       splitPane.setDividerLocation(250);   
205       
206       this.getContentPane().add(splitPane, BorderLayout.CENTER);
207       this.getContentPane().setSize(800, 500);
208       this.setSize(800,500);
209       this.setVisible(true);        
210 
211     }  catch (Exception e) { 
212       this.logger.error("Error initializing the application: ", e);
213       e.printStackTrace(System.err);
214     }
215  
216   }  
217 
218 
219   public void initLogger() { 
220     taos = new TextAreaOutputStream();
221     try {
222       final Hierarchy hierarchy = new Hierarchy();
223       PatternFormatter formatter = 
224         new PatternFormatter("[%-10.10{category}]: %{message}\n");
225       
226       hierarchy.setDefaultLogTarget(
227         new StreamTarget(taos, formatter));
228       hierarchy.setDefaultPriority(Priority.ERROR);  
229       oldLogger = hierarchy.getLoggerFor("toska");  
230       
231       
232       final ClassLoader cl = KeysAdmin.class.getClassLoader();
233       final InputStream is = 
234         cl.getResourceAsStream("it/rabellino/toska/logkit.xconf");
235       
236       final DefaultConfigurationBuilder builder =
237         new DefaultConfigurationBuilder();
238       Configuration config = builder.build(is);          
239   
240       final LogKitLoggerManager lkManager = 
241         new LogKitLoggerManager(hierarchy);
242  
243       DefaultContext context = new DefaultContext();
244       
245       context.put("console", taos);
246       
247       lkManager.contextualize(context);
248       lkManager.configure(config);
249       
250  
251       
252       Logger logger = lkManager.getLoggerForCategory("toska");
253       this.enableLogging(logger);
254     } catch (Exception e) {
255       System.err.println("Fatal error: could not configure the application" +
256         e.getMessage());
257       e.printStackTrace(System.err);
258       System.exit(1);
259     }
260     
261             
262   }  
263   
264  
265   /**
266    * @see Contextualizable#contextualize(Context)
267    */
268   public void contextualize(Context context) throws ContextException {
269     this.context = (DefaultContext)context;
270     this.context.put(Constants.MAIN, this);
271   }
272 
273    
274   /**
275    * Gets the splitPane so that components can update it.
276    * @return Returns the JSplitPane
277    */
278   public JSplitPane getSplitPane() {
279     return splitPane;
280   }
281 
282   public static void main(String[] args) {
283   
284     ClassLoader cl = KeysAdmin.class.getClassLoader();    
285     ImageIcon splashicon = new ImageIcon(cl.getResource("resources/toska.jpg"));   
286   
287     SplashScreen splash = new SplashScreen(splashicon, null, 7500);
288     
289     ExcaliburComponentManager manager = new ExcaliburComponentManager();
290     DefaultContext context = new DefaultContext();
291     
292     manager.contextualize(context);
293     KeysAdmin admin = new KeysAdmin();
294     admin.compose(manager);
295     
296     
297     DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
298 
299     try {
300 
301       InputStream is = cl.getResourceAsStream("resources/toska.xconf");
302 
303 
304       Configuration toskaConfig = builder.build(is);
305       
306       admin.configure(toskaConfig);
307       admin.contextualize(context);
308       admin.initialize();
309 
310     } catch (Exception e) { 
311       e.printStackTrace(System.err);
312 
313     }
314   }
315 
316 }