KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > monitor > client > AbstractClientDataCollector


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s):
23  */

24
25 package org.objectweb.cjdbc.common.monitor.client;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.cjdbc.common.exceptions.DataCollectorException;
30 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
31 import org.objectweb.cjdbc.controller.core.Controller;
32 import org.objectweb.cjdbc.controller.monitoring.datacollector.DataCollector;
33 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
34 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread;
35
36 /**
37  * Collects information about C-JDBC clients. TODO: Implements proper client
38  * data collection. This is not used at the moment.
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
41  */

42 public abstract class AbstractClientDataCollector extends AbstractDataCollector
43 {
44   private String JavaDoc virtualDatabaseName;
45   private String JavaDoc clientId;
46   private int clientIndex;
47
48   /**
49    * @param virtualDatabaseName of the virtualdatabase
50    * @param clientId for the client
51    * @throws DataCollectorException if cannot access client
52    */

53   public AbstractClientDataCollector(String JavaDoc virtualDatabaseName, String JavaDoc clientId)
54       throws DataCollectorException
55   {
56     super();
57     this.virtualDatabaseName = virtualDatabaseName;
58     this.clientId = clientId;
59     setClientIndex();
60   }
61
62   private Object JavaDoc setClientIndex() throws DataCollectorException
63   {
64     VirtualDatabase vdb = ((Controller) controller)
65         .getVirtualDatabase(virtualDatabaseName);
66     ArrayList JavaDoc activeThreads = vdb.getActiveThreads();
67     int size = activeThreads.size();
68     VirtualDatabaseWorkerThread client = null;
69     int index = 0;
70     for (index = 0; index < size; index++)
71     {
72       client = ((VirtualDatabaseWorkerThread) activeThreads.get(index));
73       if (client.getUser().equals(clientId))
74         break;
75       else
76         client = null;
77     }
78
79     if (client == null)
80       throw new DataCollectorException(DataCollector.CLIENT_NOT_FOUND);
81     else
82     {
83       this.clientIndex = index;
84       return client;
85     }
86   }
87
88   private Object JavaDoc checkClientIndex() throws DataCollectorException
89   {
90     VirtualDatabase vdb = ((Controller) controller)
91         .getVirtualDatabase(virtualDatabaseName);
92     ArrayList JavaDoc activeThreads = vdb.getActiveThreads();
93     VirtualDatabaseWorkerThread client = (VirtualDatabaseWorkerThread) activeThreads
94         .get(clientIndex);
95     if (client.getUser().equals(clientId))
96       return client;
97     else
98     {
99       return setClientIndex();
100     }
101   }
102
103   /**
104    * @see org.objectweb.cjdbc.common.monitor.AbstractDataCollector#collectValue()
105    */

106   public long collectValue() throws DataCollectorException
107   {
108     VirtualDatabaseWorkerThread client = (VirtualDatabaseWorkerThread) checkClientIndex();
109     return this.getValue(client);
110   }
111
112   /**
113    * We have the client object so let's get the value we want from ot
114    *
115    * @param client as an object to allow it through RMI, but IS a
116    * <code>VirtualDatabaseWorkerThread</code>
117    * @return the collected value
118    */

119   public abstract long getValue(Object JavaDoc client);
120
121   /**
122    * @see org.objectweb.cjdbc.common.monitor.AbstractDataCollector#getTargetName()
123    */

124   public String JavaDoc getTargetName()
125   {
126     return clientId;
127   }
128 }
129
Popular Tags