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 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 }