001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.domains.enterprisemodel.impl;
016:
017: import java.util.HashMap;
018: import java.util.HashSet;
019: import java.util.Iterator;
020:
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024:
025: import com.metaboss.enterprise.bo.BOException;
026: import com.metaboss.enterprise.bo.BOIllegalArgumentException;
027: import com.metaboss.enterprise.bo.BOInvalidOperationForReadOnlyObjectException;
028: import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
029: import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
030: import com.metaboss.enterprise.ps.PSException;
031: import com.metaboss.sdlctools.domains.enterprisemodel.BOService;
032: import com.metaboss.sdlctools.domains.enterprisemodel.BOServiceOperationList;
033: import com.metaboss.sdlctools.domains.enterprisemodel.BOServiceServiceimplementationList;
034: import com.metaboss.sdlctools.domains.enterprisemodel.BOServicemodule;
035: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSService;
036: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STService;
037: import com.metaboss.sdlctools.types.enterprisemodel.ZipArchive;
038: import com.oldboss.framework.bo.BOTransaction;
039: import com.oldboss.framework.bo.impl.BOObjectImpl;
040:
041: public class BOServiceImpl extends BOObjectImpl implements BOService {
042: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
043: private BOServicemodule mServicemodule = null;
044: private String mServiceRef = null;
045: private STService mDetails = null;
046: private HashMap mImplementationSources = new HashMap();
047: private HashSet mImplementationSourcesNeedUpdating = new HashSet();
048:
049: /* Instance creator */
050: public static BOService createInstanceForExisting(
051: BOMetaBossDomainImpl pMetaBossDomainImpl, String pServiceRef)
052: throws BOException {
053: // See if we have this object in cache
054: BOServiceImpl lImpl = (BOServiceImpl) pMetaBossDomainImpl
055: .retrieveExistingBOInstance(BOService.class,
056: pServiceRef);
057: if (lImpl != null)
058: return lImpl;
059: lImpl = new BOServiceImpl();
060: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
061: lImpl.mServiceRef = pServiceRef;
062: lImpl.setupForExisting();
063: pMetaBossDomainImpl.saveNewBOInstance(BOService.class,
064: pServiceRef, lImpl);
065: return lImpl;
066: }
067:
068: /* Instance creator */
069: public static BOService createInstanceForNew(
070: BOTransaction pTransaction,
071: BOMetaBossDomainImpl pMetaBossDomainImpl, String pServiceRef)
072: throws BOException {
073: // See if we have this object in cache already
074: BOServiceImpl lImpl = (BOServiceImpl) pMetaBossDomainImpl
075: .retrieveExistingBOInstance(BOService.class,
076: pServiceRef);
077: if (lImpl != null)
078: throw new BOIllegalArgumentException(
079: "Service already exists. ServiceRef: "
080: + pServiceRef);
081: lImpl = new BOServiceImpl();
082: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
083: lImpl.mServiceRef = pServiceRef;
084: lImpl.mDetails = new STService();
085: lImpl.mDetails.ServiceRef = pServiceRef;
086: lImpl.setupForNew(pTransaction);
087: pMetaBossDomainImpl.saveNewBOInstance(BOService.class,
088: pServiceRef, lImpl);
089: return lImpl;
090: }
091:
092: /* Private constructor restricts instance creation from outside */
093: private BOServiceImpl() throws BOException {
094: }
095:
096: /* Retrieves unique reference */
097: public String getRef() throws BOException {
098: return mServiceRef;
099: }
100:
101: /* Retrieves entity name */
102: public String getName() throws BOException {
103: String lServiceRef = getRef();
104: if (lServiceRef.lastIndexOf(".") < 0)
105: throw new BOException("Invalid BOService reference : "
106: + getRef());
107: return lServiceRef.substring(lServiceRef.lastIndexOf(".") + 1);
108: }
109:
110: /* Retrieves description */
111: public String getDescription() throws BOException {
112: loadDetailsIfNecessary();
113: return mDetails.Description;
114: }
115:
116: /** Sets description */
117: public void setDescription(String pDescription) throws BOException {
118: if (!isBeingEdited())
119: throw new BOInvalidOperationForReadOnlyObjectException();
120: mDetails.Description = pDescription;
121: }
122:
123: /* Retrieves parent servicemodule */
124: public BOServicemodule getServicemodule() throws BOException {
125: if (mServicemodule == null) {
126: String lServiceRef = getRef();
127: if (lServiceRef.lastIndexOf(".") < 0)
128: throw new BOException("Invalid BOService reference : "
129: + getRef());
130: mServicemodule = BOServicemoduleImpl
131: .createInstanceForExisting(mMetaBossDomainImpl,
132: lServiceRef.substring(0, lServiceRef
133: .lastIndexOf(".")));
134: }
135: return mServicemodule;
136: }
137:
138: /* Retrieves list of operations */
139: public BOServiceOperationList getOperations() throws BOException {
140: loadDetailsIfNecessary();
141: return BOServiceOperationListImpl.createInstanceForExisting(
142: mMetaBossDomainImpl, this );
143: }
144:
145: /* Retrieves the list of implementations */
146: public BOServiceServiceimplementationList getImplementations()
147: throws BOException {
148: loadDetailsIfNecessary();
149: return BOServiceServiceimplementationListImpl
150: .createInstanceForExisting(mMetaBossDomainImpl, this );
151: }
152:
153: protected void loadDetailsIfNecessary() throws BOException {
154: if (mDetails == null) {
155: try {
156: // Get the instance of the enterprise ps home via jndi
157: Context ctx = new InitialContext();
158: PSService lPs = (PSService) ctx
159: .lookup(PSService.COMPONENT_URL);
160:
161: mDetails = lPs.getService(mServiceRef);
162: if (mDetails == null)
163: throw new BOException(
164: "Service not found. ServiceRef:"
165: + mServiceRef);
166: } catch (NamingException e) {
167: throw new BONamingAndDirectoryServiceInvocationException(
168: "", e);
169: } catch (PSException e) {
170: throw new BOPersistenceServiceInvocationException("", e);
171: }
172: }
173: }
174:
175: protected void loadImplementationSourceIfNecessary(
176: String pSourceName) throws BOException {
177: if (mImplementationSources.containsKey(pSourceName) == false
178: || mImplementationSources.get(pSourceName) == null) {
179: try {
180: // Get the instance of the enterprise ps home via jndi
181: Context ctx = new InitialContext();
182: PSService lPs = (PSService) ctx
183: .lookup(PSService.COMPONENT_URL);
184:
185: ZipArchive lArchive = lPs.getImplementationSource(
186: mServiceRef, pSourceName);
187: if (lArchive == null)
188: throw new BOException(
189: "Implementation source for the Service not found. ServiceRef : "
190: + mServiceRef + ". Source Name : "
191: + pSourceName);
192: mImplementationSources.put(pSourceName, lArchive);
193: } catch (NamingException e) {
194: throw new BONamingAndDirectoryServiceInvocationException(
195: "", e);
196: } catch (PSException e) {
197: throw new BOPersistenceServiceInvocationException("", e);
198: }
199: }
200: }
201:
202: protected void onBeginEdit() throws BOException {
203: // Fully load the object
204: loadDetailsIfNecessary();
205: mImplementationSourcesNeedUpdating.clear();
206: }
207:
208: /* Encapsulates commit action by this object */
209: protected void onCommitCreation() throws BOException {
210: try {
211: // Get the instance of the enterprise ps home via jndi
212: Context ctx = new InitialContext();
213: PSService lPs = (PSService) ctx
214: .lookup(PSService.COMPONENT_URL);
215: lPs.insertService(mDetails);
216: // Update sources
217: Iterator lIter = mImplementationSourcesNeedUpdating
218: .iterator();
219: while (lIter.hasNext()) {
220: lPs.updateImplementationSource(mServiceRef,
221: (ZipArchive) mImplementationSources
222: .get((String) lIter.next()));
223: }
224: } catch (NamingException e) {
225: throw new BONamingAndDirectoryServiceInvocationException(
226: "", e);
227: } catch (PSException e) {
228: throw new BOPersistenceServiceInvocationException("", e);
229: }
230: }
231:
232: /* Encapsulates commit action by this object */
233: protected void onCommitUpdate() throws BOException {
234: try {
235: // Get the instance of the enterprise ps home via jndi
236: Context ctx = new InitialContext();
237: PSService lPs = (PSService) ctx
238: .lookup(PSService.COMPONENT_URL);
239: // Update sources
240: Iterator lIter = mImplementationSourcesNeedUpdating
241: .iterator();
242: while (lIter.hasNext()) {
243: lPs.updateImplementationSource(mServiceRef,
244: (ZipArchive) mImplementationSources
245: .get((String) lIter.next()));
246: }
247: } catch (NamingException e) {
248: throw new BONamingAndDirectoryServiceInvocationException(
249: "", e);
250: } catch (PSException e) {
251: throw new BOPersistenceServiceInvocationException("", e);
252: }
253: }
254:
255: /* Encapsulates an action after successfull commit */
256: public void onCommitSucceeded() throws BOException {
257: mImplementationSourcesNeedUpdating.clear();
258: }
259: }
|