View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.provider;
17  
18  import java.rmi.dgc.VMID;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.jdiagnose.concurrent.ConcurrentHashMap;
27  
28  /***
29   * @author jmccrindle
30   */
31  public class DefaultSystemProvider implements SystemProvider, MutableSystemProvider {
32  
33      private Map holders = new ConcurrentHashMap();
34      private List holdersList = Collections.synchronizedList(new ArrayList());
35      
36      /* (non-Javadoc)
37       * @see org.jdiagnose.remote.provider.SystemProvider#getHolder(java.lang.String)
38       */
39      public SystemHolder getHolder(String id) {
40          synchronized(holders) {
41              return (SystemHolder) holders.get(id);
42          }
43      }
44  
45      /* (non-Javadoc)
46       * @see org.jdiagnose.remote.provider.MutableSystemProvider#addHolder(org.jdiagnose.remote.provider.SystemHolder)
47       */
48      public void addHolder(SystemHolder holder) {
49          synchronized(holders) {
50              holders.put(holder.getId(), holder);
51              holdersList.add(holder);
52          }
53      }
54  
55      /* (non-Javadoc)
56       * @see org.jdiagnose.remote.provider.MutableSystemProvider#remoteHolder(java.lang.String)
57       */
58      public void removeHolder(String id) {
59          synchronized(holders) {
60              SystemHolder holder = (SystemHolder) holders.remove(id);
61              holdersList.remove(holder);
62          }
63      }
64  
65      public String nextId() {
66          return new VMID().toString();
67      }
68  
69      /* (non-Javadoc)
70       * @see org.jdiagnose.remote.provider.SystemProvider#getHolders()
71       */
72      public Collection getHolders() {
73          synchronized(holders) {
74              return holders.values();
75          }
76      }
77  
78      public void setHoldersList(List holdersList) {
79          synchronized(holders) {
80              this.holdersList.clear();
81              this.holdersList.addAll(holdersList);
82              this.holders.clear();
83              for (Iterator holderIterator = this.holdersList.iterator(); holderIterator.hasNext();) {
84                  SystemHolder holder = (SystemHolder) holderIterator.next();
85                  holders.put(holder.getId(), holder);
86              }
87          }
88      }
89  }