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.models.impl.metabossmodel.designlibrarymodel;
016:
017: import java.io.File;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.zip.ZipEntry;
024: import java.util.zip.ZipInputStream;
025:
026: import javax.jmi.reflect.ConstraintViolationException;
027:
028: import org.netbeans.mdr.storagemodel.StorableObject;
029:
030: import com.metaboss.sdlctools.models.ModelRepository;
031: import com.metaboss.sdlctools.models.ModelRepositoryException;
032: import com.metaboss.sdlctools.models.ModelRepositoryRuntimeException;
033: import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
034: import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementResolverImpl;
035: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
036: import com.metaboss.sdlctools.models.metabossmodel.ModelElementAttachment;
037: import com.metaboss.sdlctools.models.metabossmodel.ModelElementAttachmentUtils;
038: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.IncludedEnterpriseReference;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
040: import com.metaboss.util.ByteUtils;
041: import com.metaboss.util.DirectoryUtils;
042: import com.metaboss.util.StreamUtils;
043:
044: public abstract class IncludedEnterpriseReferenceImpl extends
045: ModelElementImpl implements IncludedEnterpriseReference {
046: private static final String MODELELEMENT_ATTACHMENT_NAME = "ModelArchive";
047: private static final String MODEL_FILE_PREFIX_IN_ARCHIVE = "model/metaboss/";
048: private static final String MODEL_FILE_NAME = "Model.xml";
049:
050: // Required constructor
051: protected IncludedEnterpriseReferenceImpl(StorableObject storable) {
052: super (storable);
053: }
054:
055: // Forward declaration of automatically generated setter method
056: protected abstract void super _setModelArchiveUrl(String pUrl);
057:
058: /** Sets the value of modelArchiveURL attribute */
059: public void setModelArchiveUrl(String pModelArchiveUrl) {
060: // First see if there is already Url and Enterprise stored
061: // and delete it if it is not the same
062: if (getModelArchiveUrl() != null) {
063: if (!getModelArchiveUrl().equals(pModelArchiveUrl)) {
064: try {
065: Enterprise lOldEnterprise = getEnterprise();
066: ModelElementAttachment lOldAttachment = getAttachment(MODELELEMENT_ATTACHMENT_NAME);
067: ModelElementAttachmentUtils
068: .deleteAttachmentData(lOldAttachment);
069: lOldAttachment.refDelete();
070: lOldEnterprise.refDelete();
071: } catch (ModelRepositoryException e) {
072: throw new ModelRepositoryRuntimeException(e);
073: }
074: } else {
075: // TODO: Latter on we will need to implement some sort of checksum verification
076: // but for now lets just say that they are the same
077: return;
078: }
079: }
080: // We need to do a number of things
081: try {
082: URL lModelArchiveUrl = new URL(pModelArchiveUrl);
083: // 1- open the model in the repository as a separate model and copy the Enterprise and all subelements under here
084: {
085: File lTempDir = null;
086: try {
087: File lModelFile = null;
088: InputStream lInputStream = null;
089: try {
090: if ((lInputStream = lModelArchiveUrl
091: .openStream()) == null)
092: throw new ConstraintViolationException(
093: null,
094: null,
095: "The ModelArchiveUrl attribute must contain URL, which is reachable at the time when it is being set. URL '"
096: + pModelArchiveUrl
097: + "' is not reachable");
098: ZipInputStream lZipInputStream = null;
099: try {
100: lZipInputStream = new ZipInputStream(
101: lInputStream);
102: for (ZipEntry lZipEntry = lZipInputStream
103: .getNextEntry(); lZipEntry != null; lZipEntry = lZipInputStream
104: .getNextEntry()) {
105: // We are only interested in extracting the bits and pieces of the metaboss model
106: String lEntryName = lZipEntry.getName();
107: if ((lEntryName
108: .startsWith(MODEL_FILE_PREFIX_IN_ARCHIVE))
109: && (lEntryName
110: .equals(MODEL_FILE_PREFIX_IN_ARCHIVE) == false)) {
111: lEntryName = lEntryName
112: .substring(MODEL_FILE_PREFIX_IN_ARCHIVE
113: .length());
114: if (lTempDir == null)
115: lTempDir = DirectoryUtils
116: .createTempDir(
117: "includedEnterprise",
118: "");
119: File lTargetFile = new File(
120: lTempDir.getAbsolutePath()
121: + File.separator
122: + lEntryName);
123: // Keep the top model file handler, so we can open it
124: if (lEntryName
125: .equals(MODEL_FILE_NAME))
126: lModelFile = lTargetFile;
127: // Save the file where it belongs
128: DirectoryUtils
129: .ensureThereIsDirectory(lTargetFile
130: .getParentFile()
131: .getAbsolutePath());
132: FileOutputStream lFileOutputStream = null;
133: try {
134: lFileOutputStream = new FileOutputStream(
135: lTargetFile, false);
136: StreamUtils.copyData(
137: lZipInputStream,
138: lFileOutputStream,
139: lZipEntry.getSize());
140: } finally {
141: if (lFileOutputStream != null)
142: lFileOutputStream.close();
143: }
144: }
145: }
146: } finally {
147: if (lZipInputStream != null)
148: lZipInputStream.close();
149: }
150: } finally {
151: if (lInputStream != null)
152: lInputStream.close();
153: }
154: if (lModelFile == null)
155: throw new ConstraintViolationException(
156: null,
157: null,
158: "The ModelArchiveUrl attribute must point to the model archive, which contains "
159: + MODEL_FILE_PREFIX_IN_ARCHIVE
160: + MODEL_FILE_NAME
161: + " model definition file. URL '"
162: + pModelArchiveUrl
163: + "' does not conform to this rule.");
164: // Now we are ready to open the model
165: ModelRepository lRepository = getModelRepository();
166: try {
167: lRepository
168: .openModel(
169: pModelArchiveUrl,
170: lModelFile,
171: ModelRepository.METAMODEL_NAME_METABOSS);
172: MetaBossModelPackage lMetaBossModelPackage = (MetaBossModelPackage) lRepository
173: .getModelExtent(pModelArchiveUrl);
174: Enterprise lEnterpriseToImport = (Enterprise) lMetaBossModelPackage
175: .getModelElement().getByRef(
176: "Enterprise");
177: Enterprise lImportedEnterprise = (Enterprise) lRepository
178: .copyModelElement(
179: lEnterpriseToImport,
180: lRepository
181: .getOwnerModelName(this ),
182: true, ModelElementResolverImpl
183: .getInstance());
184: setEnterprise(lImportedEnterprise);
185: } finally {
186: if (lRepository.containsModel(pModelArchiveUrl))
187: lRepository.closeModel(pModelArchiveUrl);
188: }
189: } finally {
190: if (lTempDir != null)
191: DirectoryUtils.deleteDirectory(lTempDir);
192: }
193: }
194: // 2- load the model archive from the specified Url and save it as an attachment to this element
195: {
196: InputStream lInputStream = null;
197: try {
198: if ((lInputStream = lModelArchiveUrl.openStream()) == null)
199: throw new ConstraintViolationException(
200: null,
201: null,
202: "The ModelArchiveUrl attribute must contain URL, which is reachable at the time when it is being set. URL '"
203: + pModelArchiveUrl
204: + "' is not reachable");
205: ModelElementAttachment lAttachment = ((MetaBossModelPackage) refOutermostPackage())
206: .getModelElementAttachment()
207: .createModelElementAttachment();
208: lAttachment.setName(MODELELEMENT_ATTACHMENT_NAME);
209: lAttachment
210: .setDescription("The imported model archive");
211: ModelElementAttachmentUtils.setAttachmentData(
212: lAttachment, ByteUtils
213: .readByteStreamFully(lInputStream));
214: } finally {
215: if (lInputStream != null)
216: lInputStream.close();
217: }
218: }
219: } catch (ModelRepositoryException e) {
220: throw new ModelRepositoryRuntimeException(e);
221: } catch (MalformedURLException e) {
222: ConstraintViolationException lException = new ConstraintViolationException(
223: null, null,
224: "The ModelArchiveUrl attribute must contain valid URL");
225: lException.initCause(e);
226: throw lException;
227: } catch (IOException e) {
228: throw new ModelRepositoryRuntimeException(
229: "Caught IOException while trying to import the model archive",
230: e);
231: }
232: // Call the superclass attribute setter
233: super_setModelArchiveUrl(pModelArchiveUrl);
234:
235: }
236: }
|