001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.continuent.sequoia.controller.virtualdatabase.activity;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Map.Entry;
023:
024: /**
025: * An ActivityService is responsible to centralize "activities" information related
026: * to virtual databases
027: */
028: public final class ActivityService {
029: private static final ActivityService instance = new ActivityService();
030:
031: /*
032: * vdbs is a Map<String,Map> where:
033: * - the key is a String representing the name of a virtual database
034: * - the value is a Map<Object,Boolean> where
035: * - the key is an Object representing a vdb member (likely a Member)
036: * - the value is a Boolean used to flag if the vdb has had activity since the given
037: * member was flagged as unreachable:
038: * * TRUE means *some* activity since the member was flagged as unreachable
039: * * FALSE means *no* activity since the member was flagged as unreachable
040: */
041: private final Map/*<String, Map>*/vdbs = new HashMap();
042:
043: private ActivityService() {
044: }
045:
046: /**
047: * Returns the instance of the ActivityService.
048: * This instance is global to the Controller.
049: *
050: * @return the instance of the ActivityService
051: */
052: public static final ActivityService getInstance() {
053: return instance;
054: }
055:
056: /**
057: * Stops the activity service
058: */
059: public synchronized void stop() {
060: vdbs.clear();
061: }
062:
063: /**
064: * Resets the activity information related to the given virtual database.
065: *
066: * @param vdb the name of a virtual database
067: */
068: public synchronized void reset(String vdb) {
069: vdbs.remove(vdb);
070: }
071:
072: /**
073: * Notifies that there has been activity related to the given virtual database
074: *
075: * @param vdb the name of the virtual database
076: */
077: public void notifyActivityFor(String vdb) {
078: Map unreachableMembers = (Map) vdbs.get(vdb);
079: if (unreachableMembers != null) {
080: Iterator iter = unreachableMembers.entrySet().iterator();
081: while (iter.hasNext()) {
082: Entry entry = (Entry) iter.next();
083: // This is only writing an object reference, which is guaranteed to be
084: // an atomic operation by the JLS (beginning of chapter 17. Threads and
085: // locks). No need to synchronize here.
086: entry.setValue(Boolean.TRUE);
087: }
088: }
089: }
090:
091: /**
092: * Flags the given member as unreachable for the given virtual database.
093: *
094: * @param vdb the name of a virtual database
095: * @param member an Object representing the member of a virtual database
096: */
097: public synchronized void addUnreachableMember(String vdb,
098: Object member) {
099: Map unreachableMembers = (Map) vdbs.get(vdb);
100: if (unreachableMembers == null) {
101: // 1st unreachable member for the vdb -> lazy creation of the unreachableMembers Map
102: unreachableMembers = new HashMap();
103: vdbs.put(vdb, unreachableMembers);
104: }
105: // set the boolean to false only if the member is not already flagged as unreachable
106: if (!unreachableMembers.containsKey(member)) {
107: unreachableMembers.put(member, Boolean.FALSE);
108: }
109: }
110:
111: /**
112: * Checks if there has been some activities for the given virtual database
113: * since the member was flagged as unreachable.
114: *
115: * @param vdb the name of a virtual database
116: * @param member an Object representing the member of a virtual database
117: * @return <code>true</code> if there has been some activities for the vdb
118: * since the member was flagged as unreachable, <code>false</code> in any
119: * other cases
120: */
121: public boolean hasActivitySinceUnreachable(String vdb, Object member) {
122: Map unreachableMembers = (Map) vdbs.get(vdb);
123: if (unreachableMembers == null) {
124: return false;
125: }
126: Boolean activity = (Boolean) unreachableMembers.get(member);
127: if (activity == null) {
128: return false;
129: }
130: return activity.booleanValue();
131: }
132: }
|