001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.jndi;
023:
024: import java.io.Serializable;
025: import java.util.Collection;
026:
027: import javax.naming.Name;
028: import javax.naming.NamingException;
029:
030: import org.jnp.interfaces.Naming;
031:
032: import org.jboss.ha.framework.interfaces.HAPartition;
033: import org.jboss.ha.framework.interfaces.HAPartition.HAPartitionStateTransfer;
034: import org.jboss.logging.Logger;
035:
036: /**
037: * This class extends the JNP JNDI implementation.
038: * binds and unbinds will be distributed to all members of the cluster
039: * that are running HAJNDI.
040: * lookups will look for Names in HAJNDI then delegate to the local InitialContext
041: *
042: * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
043: * @author Scott.Stark@jboss.org
044: * @version $Revision: 57188 $
045: */
046: public class HAJNDI implements HAPartitionStateTransfer, Serializable,
047: org.jnp.interfaces.Naming {
048: /** @since 1.12.2.4, jboss-3.2.2 */
049: static final long serialVersionUID = -6277328603304171620L;
050:
051: // Attributes --------------------------------------------------------
052: private static Logger log = Logger.getLogger(HAJNDI.class);
053:
054: protected HAPartition partition;
055: protected TreeHead delegate;
056: protected Naming haStub;
057:
058: // Constructor --------------------------------------------------------
059:
060: public HAJNDI(HAPartition partition) throws NamingException {
061: if (partition == null)
062: throw new IllegalArgumentException("Null partition");
063: this .partition = partition;
064: delegate = new TreeHead();
065: delegate.setPartition(this .partition);
066: delegate.setHARMIHead(this );
067: }
068:
069: // Public --------------------------------------------------------
070:
071: public void init() throws Exception {
072: log.debug("subscribeToStateTransferEvents");
073: partition.subscribeToStateTransferEvents("HAJNDI", this );
074: delegate.init();
075: }
076:
077: public void stop() throws Exception {
078: delegate.stop();
079: }
080:
081: public void destroy() throws Exception {
082: delegate.destroy();
083: partition.unsubscribeFromStateTransferEvents("HAJNDI", this );
084: }
085:
086: public void setHAStub(Naming stub) {
087: this .haStub = stub;
088: }
089:
090: public Naming getHAStub() {
091: return this .haStub;
092: }
093:
094: // HAPartition.HAPartitionStateTransfer Implementation --------------------------------------------------------
095:
096: public Serializable getCurrentState() {
097: if (log.isTraceEnabled())
098: log.trace("getCurrentState called");
099: return delegate;
100: }
101:
102: public void setCurrentState(Serializable newState) {
103: if (log.isTraceEnabled())
104: log.trace("setCurrentState called");
105:
106: try {
107: delegate.stop();
108: delegate = (TreeHead) newState;
109: delegate.setPartition(this .partition);
110: delegate.setHARMIHead(this );
111: delegate.init();
112: } catch (Exception failed) {
113: log.warn("Problem restoring state to HA-JNDI", failed);
114: }
115: }
116:
117: // Naming implementation -----------------------------------------
118:
119: public synchronized void bind(Name name, Object obj,
120: String className) throws NamingException {
121: delegate.bind(name, obj, className);
122: }
123:
124: public synchronized void rebind(Name name, Object obj,
125: String className) throws NamingException {
126: delegate.rebind(name, obj, className);
127: }
128:
129: public synchronized void unbind(Name name) throws NamingException {
130: delegate.unbind(name);
131: }
132:
133: public Object lookup(Name name) throws NamingException {
134: return delegate.lookup(name);
135: }
136:
137: public Collection list(Name name) throws NamingException {
138: return delegate.list(name);
139: }
140:
141: public Collection listBindings(Name name) throws NamingException {
142: return delegate.listBindings(name);
143: }
144:
145: public javax.naming.Context createSubcontext(Name name)
146: throws NamingException {
147: return delegate.createSubcontext(name);
148: }
149: }
|