001
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
026 public abstract class BaseWelder implements Welder {
027
028 public BaseWelder() {
029
030
031
032
033
034
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 }