001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.nio.intraband.welder;
016    
017    import com.liferay.portal.kernel.nio.intraband.Intraband;
018    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
019    
020    import java.io.IOException;
021    import java.io.ObjectInputStream;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public abstract class BaseWelder implements Welder {
027    
028            public BaseWelder() {
029    
030                    // Assignments have to stay in the constructor because we need to
031                    // differentiate between a constructor created object and a
032                    // deserialization created object. Only the constructor created object
033                    // needs to assign values. The deserialization created object gets its
034                    // values from the original object.
035    
036                    server = true;
037            }
038    
039            @Override
040            public synchronized void destroy() throws IOException {
041                    if (state != State.WELDED) {
042                            throw new IllegalStateException(
043                                    "Unable to destroy a welder with state " + state);
044                    }
045    
046                    registrationReference.cancelRegistration();
047    
048                    doDestroy();
049    
050                    state = State.DESTROYED;
051            }
052    
053            @Override
054            public synchronized RegistrationReference weld(Intraband intraband)
055                    throws IOException {
056    
057                    if (state != State.CREATED) {
058                            throw new IllegalStateException(
059                                    "Unable to weld a welder with state " + state);
060                    }
061    
062                    if (server) {
063                            registrationReference = weldServer(intraband);
064                    }
065                    else {
066                            registrationReference = weldClient(intraband);
067                    }
068    
069                    state = State.WELDED;
070    
071                    return registrationReference;
072            }
073    
074            protected abstract void doDestroy() throws IOException;
075    
076            protected abstract RegistrationReference weldClient(Intraband intraband)
077                    throws IOException;
078    
079            protected abstract RegistrationReference weldServer(Intraband intraband)
080                    throws IOException;
081    
082            protected transient RegistrationReference registrationReference;
083            protected final transient boolean server;
084            protected transient State state = State.CREATED;
085    
086            protected static enum State {
087    
088                    CREATED, DESTROYED, WELDED
089    
090            }
091    
092            private void readObject(ObjectInputStream objectInputStream)
093                    throws ClassNotFoundException, IOException {
094    
095                    objectInputStream.defaultReadObject();
096    
097                    state = State.CREATED;
098            }
099    
100    }