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            public synchronized void destroy() throws IOException {
040                    if (state != State.WELDED) {
041                            throw new IllegalStateException(
042                                    "Unable to destroy a welder with state " + state);
043                    }
044    
045                    registrationReference.cancelRegistration();
046    
047                    doDestroy();
048    
049                    state = State.DESTROYED;
050            }
051    
052            public synchronized RegistrationReference weld(IntraBand intraBand)
053                    throws IOException {
054    
055                    if (state != State.CREATED) {
056                            throw new IllegalStateException(
057                                    "Unable to weld a welder with state " + state);
058                    }
059    
060                    if (server) {
061                            registrationReference = weldServer(intraBand);
062                    }
063                    else {
064                            registrationReference = weldClient(intraBand);
065                    }
066    
067                    state = State.WELDED;
068    
069                    return registrationReference;
070            }
071    
072            protected abstract void doDestroy() throws IOException;
073    
074            protected abstract RegistrationReference weldClient(IntraBand intraBand)
075                    throws IOException;
076    
077            protected abstract RegistrationReference weldServer(IntraBand intraBand)
078                    throws IOException;
079    
080            protected transient RegistrationReference registrationReference;
081            protected final transient boolean server;
082            protected transient State state = State.CREATED;
083    
084            protected static enum State {
085    
086                    CREATED, DESTROYED, WELDED
087    
088            }
089    
090            private void readObject(ObjectInputStream objectInputStream)
091                    throws ClassNotFoundException, IOException {
092    
093                    objectInputStream.defaultReadObject();
094    
095                    state = State.CREATED;
096            }
097    
098    }