001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.cluster;
030:
031: import com.caucho.loader.EnvironmentLocal;
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037:
038: /**
039: * Defines a group of clusters. Normally, the group is the entire
040: * set of clusters in the server.
041: * subgroups of servers.
042: */
043: public class ClusterGroup {
044: private static final L10N L = new L10N(ClusterGroup.class);
045: private static final Logger log = Log.open(ClusterGroup.class);
046:
047: static protected final EnvironmentLocal<ClusterGroup> _clusterGroupLocal = new EnvironmentLocal<ClusterGroup>();
048:
049: private final ArrayList<Cluster> _clusterList = new ArrayList<Cluster>();
050:
051: private ClusterGroup() {
052: }
053:
054: /**
055: * Returns the cluster group for the current level.
056: */
057: public static ClusterGroup getClusterGroup() {
058: return _clusterGroupLocal.get();
059: }
060:
061: /**
062: * Returns the cluster group for the current level.
063: */
064: public static ClusterGroup createClusterGroup() {
065: ClusterGroup group = _clusterGroupLocal.getLevel();
066:
067: if (group == null) {
068: group = new ClusterGroup();
069: _clusterGroupLocal.set(group);
070: }
071:
072: return group;
073: }
074:
075: /**
076: * Adds a cluster.
077: */
078: public void addCluster(Cluster cluster) {
079: if (!_clusterList.contains(cluster))
080: _clusterList.add(cluster);
081: }
082:
083: /**
084: * Returns all the clusters.
085: */
086: public ArrayList<Cluster> getClusterList() {
087: return _clusterList;
088: }
089:
090: /**
091: * Returns the cluster with the matching name.
092: */
093: public Cluster findCluster(String id) {
094: for (int i = _clusterList.size() - 1; i >= 0; i--) {
095: Cluster cluster = _clusterList.get(i);
096:
097: if (cluster.getId().equals(id))
098: return cluster;
099: }
100:
101: return null;
102: }
103:
104: /**
105: * Finds the srun client port matching the host and port.
106: */
107: public ServerConnector findClient(String host, int port) {
108: for (int i = _clusterList.size() - 1; i >= 0; i--) {
109: Cluster cluster = _clusterList.get(i);
110:
111: ServerConnector serverConnector = cluster.findConnector(
112: host, port);
113:
114: if (serverConnector != null)
115: return serverConnector;
116: }
117:
118: return null;
119: }
120: }
|