KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > AbstractResourceService


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;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.maverick.multiplex.Request;
29 import com.maverick.util.ByteArrayWriter;
30 import com.sslexplorer.core.CoreEvent;
31 import com.sslexplorer.core.CoreListener;
32 import com.sslexplorer.core.CoreServlet;
33 import com.sslexplorer.policyframework.ResourceType;
34
35 /**
36  * Abstract {@link AgentService} implementation for dealing with
37  * the SSL-Explorer resources.
38  *
39  * @author brett
40  */

41 public abstract class AbstractResourceService implements AgentService, CoreListener {
42
43     final static Log log = LogFactory.getLog(AbstractResourceService.class);
44     
45     private int[] events;
46     private ResourceType resourceType;
47     
48     /**
49      * Constructor
50      *
51      * @param resourceType resource type
52      * @param events array of event codes to watch for
53      */

54     public AbstractResourceService(ResourceType resourceType, int[] events) {
55         CoreServlet.getServlet().addCoreListener(this);
56         this.resourceType = resourceType;
57         this.events = events;
58     }
59
60     /* (non-Javadoc)
61      * @see com.sslexplorer.core.CoreListener#coreEvent(com.sslexplorer.core.CoreEvent)
62      */

63     public void coreEvent(CoreEvent evt) {
64         // If applications change, we need to tell all of the agents
65
for(int i = 0 ; i < events.length ; i++) {
66             if(evt.getId() == events[i]) {
67                 DefaultAgentManager.getInstance().getAgentBroadcastExecutor().execute(new Runnable JavaDoc() {
68                     public void run() {
69                         Collection JavaDoc<AgentTunnel> agents = DefaultAgentManager.getInstance().getAgents();
70                         synchronized(agents) {
71                             for(AgentTunnel agent : agents) {
72                                 ByteArrayWriter baw = new ByteArrayWriter();
73                                 try {
74                                     baw.writeInt(resourceType.getResourceTypeId());
75                                     synchronized(agent) {
76                                         agent.sendRequest(new Request(AgentTunnel.UPDATE_RESOURCES_REQUEST, baw.toByteArray()), false);
77                                     }
78                                 }
79                                 catch(IOException JavaDoc ioe) {
80                                     log.warn("Failed to send resource update request to agent " + agent.getId() + ".", ioe);
81                                 }
82                             }
83                         }
84                     }
85                 });
86                 return;
87             }
88         }
89     }
90 }
91
Popular Tags