KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > ClusterGroup


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.cluster;
30
31 import com.caucho.loader.EnvironmentLocal;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * Defines a group of clusters. Normally, the group is the entire
40  * set of clusters in the server.
41  * subgroups of servers.
42  */

43 public class ClusterGroup {
44   private static final L10N L = new L10N(ClusterGroup.class);
45   private static final Logger JavaDoc log = Log.open(ClusterGroup.class);
46
47   static protected final EnvironmentLocal<ClusterGroup> _clusterGroupLocal
48     = new EnvironmentLocal<ClusterGroup>();
49
50   private final ArrayList JavaDoc<Cluster> _clusterList
51     = new ArrayList JavaDoc<Cluster>();
52
53   private ClusterGroup()
54   {
55   }
56
57   /**
58    * Returns the cluster group for the current level.
59    */

60   public static ClusterGroup getClusterGroup()
61   {
62     return _clusterGroupLocal.get();
63   }
64     
65   /**
66    * Returns the cluster group for the current level.
67    */

68   public static ClusterGroup createClusterGroup()
69   {
70     ClusterGroup group = _clusterGroupLocal.getLevel();
71
72     if (group == null) {
73       group = new ClusterGroup();
74       _clusterGroupLocal.set(group);
75     }
76
77     return group;
78   }
79
80   /**
81    * Adds a cluster.
82    */

83   public void addCluster(Cluster cluster)
84   {
85     if (! _clusterList.contains(cluster))
86       _clusterList.add(cluster);
87   }
88
89   /**
90    * Returns all the clusters.
91    */

92   public ArrayList JavaDoc<Cluster> getClusterList()
93   {
94     return _clusterList;
95   }
96
97   /**
98    * Returns the cluster with the matching name.
99    */

100   public Cluster findCluster(String JavaDoc id)
101   {
102     for (int i = _clusterList.size() - 1; i >= 0; i--) {
103       Cluster cluster = _clusterList.get(i);
104
105       if (cluster.getId().equals(id))
106     return cluster;
107     }
108     
109     return null;
110   }
111
112   /**
113    * Finds the srun client port matching the host and port.
114    */

115   public ClusterClient findClient(String JavaDoc host, int port)
116   {
117     for (int i = _clusterList.size() - 1; i >= 0; i--) {
118       Cluster cluster = _clusterList.get(i);
119
120       ClusterClient clusterClient = cluster.findClient(host, port);
121
122       if (clusterClient != null)
123     return clusterClient;
124     }
125     
126     return null;
127   }
128 }
129
Popular Tags