KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > AbstractResourceManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client;
21
22 import java.io.IOException JavaDoc;
23
24 import com.maverick.multiplex.Request;
25 import com.maverick.util.ByteArrayReader;
26 import com.maverick.util.ByteArrayWriter;
27
28 public abstract class AbstractResourceManager {
29
30     // #ifdef DEBUG
31
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AbstractResourceManager.class);
32     // #endif
33

34     // Protected instance variables
35

36     protected Agent agent;
37
38     public AbstractResourceManager(Agent agent) {
39         this.agent = agent;
40     }
41
42     public Agent getAgent() {
43         return agent;
44     }
45
46     public void getResources(int resourceType, String JavaDoc menu) {
47         try {
48             ByteArrayWriter baw = new ByteArrayWriter();
49             baw.writeInt(resourceType);
50             Request request = new Request("getResources@3sp.com", baw.toByteArray());
51             if (agent.getConnection().sendRequest(request, true)) {
52                 if(request.getRequestData()!=null) {
53                     if(agent.getGUI().isMenuExists(menu))
54                         agent.getGUI().clearMenu(menu);
55                     else
56                         agent.getGUI().addMenu(menu);
57                     ByteArrayReader reader = new ByteArrayReader(request.getRequestData());
58                     int count = (int) reader.readInt();
59                     if (count > 0) {
60                         for (int i = 0; i < count; i++) {
61                             int resourceId = (int) reader.readInt();
62                             agent.getGUI().addMenuItem(menu, new ResourceLaunchAction(resourceId, reader.readString()));
63                         }
64                     }
65                 }
66             }
67         } catch (IOException JavaDoc e) {
68             // #ifdef DEBUG
69
log.error("Failed to get resources.", e);
70             // #endif
71
}
72     }
73
74     public abstract void launchResource(int resourceId);
75
76     class ResourceLaunchAction implements AgentAction {
77
78         int resourceId;
79         String JavaDoc displayName;
80
81         ResourceLaunchAction(int resourceId, String JavaDoc displayName) {
82             this.resourceId = resourceId;
83             this.displayName = displayName;
84         }
85
86         public void actionPerformed() {
87             
88             Thread JavaDoc t = new Thread JavaDoc() {
89                 public void run() {
90                     launchResource(resourceId);
91                 }
92             };
93             t.start();
94         }
95
96         public String JavaDoc getAction() {
97             return displayName;
98         }
99
100     }
101 }
102
Popular Tags