KickJava   Java API By Example, From Geeks To Geeks.

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


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
34 import java.util.ArrayList JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Container of clusters.
39  */

40 public class ClusterContainer {
41   static protected final Logger JavaDoc log = Log.open(ClusterContainer.class);
42
43   static final EnvironmentLocal<ClusterContainer> _clusterContainerLocal =
44   new EnvironmentLocal<ClusterContainer>("caucho.cluster.container");
45   
46   private ClusterContainer _parent;
47   
48   private ArrayList JavaDoc<Cluster> _clusterList = new ArrayList JavaDoc<Cluster>();
49
50   private ClusterContainer()
51   {
52   }
53
54   /**
55    * Creates the cluster for the current level.
56    */

57   public static ClusterContainer create()
58   {
59     ClusterContainer container = _clusterContainerLocal.getLevel();
60
61     if (container == null) {
62       container = new ClusterContainer();
63       ClusterContainer parent = _clusterContainerLocal.get();
64
65       container.setParent(parent);
66       _clusterContainerLocal.set(container);
67     }
68
69     return container;
70   }
71
72   /**
73    * Creates the cluster for the current level.
74    */

75   public static ClusterContainer getLocal()
76   {
77     return _clusterContainerLocal.get();
78   }
79
80   /**
81    * Creates the cluster for the current level.
82    */

83   public static ClusterContainer getLocal(ClassLoader JavaDoc loader)
84   {
85     return _clusterContainerLocal.get(loader);
86   }
87
88   /**
89    * Sets the parent container.
90    */

91   private void setParent(ClusterContainer parent)
92   {
93     _parent = parent;
94   }
95
96   /**
97    * Gets the parent container.
98    */

99   private ClusterContainer getParent()
100   {
101     return _parent;
102   }
103
104   /**
105    * Adds a cluster.
106    */

107   public void addCluster(Cluster cluster)
108   {
109     _clusterList.add(cluster);
110   }
111
112   /**
113    * Returns the cluster list.
114    */

115   public ArrayList JavaDoc<Cluster> getClusterList()
116   {
117     return _clusterList;
118   }
119
120   /**
121    * Finds the named cluster.
122    */

123   public Cluster findCluster(String JavaDoc id)
124   {
125     for (int i = 0; i < _clusterList.size(); i++) {
126       Cluster cluster = _clusterList.get(i);
127
128       if (id.equals(cluster.getId()))
129         return cluster;
130     }
131
132     if (_parent != null)
133       return _parent.findCluster(id);
134     else
135       return null;
136   }
137
138   /**
139    * Returns the matching ports.
140    */

141   public ArrayList JavaDoc<ClusterPort> getServerPorts(String JavaDoc serverId)
142   {
143     ArrayList JavaDoc<ClusterPort> ports = new ArrayList JavaDoc<ClusterPort>();
144     
145     for (int i = 0; i < _clusterList.size(); i++) {
146       Cluster cluster = _clusterList.get(i);
147
148       ports.addAll(cluster.getServerPorts(serverId));
149     }
150
151     return ports;
152   }
153
154   public String JavaDoc toString()
155   {
156     return ("ClusterContainer[]");
157   }
158 }
159
Popular Tags