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;
016:
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.jmi.model.AggregationKindEnum;
025: import javax.jmi.model.AssociationEnd;
026: import javax.jmi.model.ModelElement;
027: import javax.jmi.model.MofClass;
028: import javax.jmi.model.Reference;
029: import javax.jmi.reflect.RefObject;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /** The utilities related to MOF */
035: public class MOFUtils {
036: // Commons Logging instance.
037: private static final Log sLogger = LogFactory
038: .getLog(MOFUtils.class);
039:
040: /** This structure keeps additional type information */
041: private static class TypeInformation {
042: private String mTypeName;
043: private List mParentReferences;// References to all possible containers
044: private List mContainedCollectionsReferences; // References to all contained collections
045: private List mContainedReferences; // References to all contained objects
046: private Map mAllReferences; // All references mapped by name
047: }
048:
049: // Map of type information per MofObject
050: private static Map sTypeInfoMap = new HashMap();
051:
052: // This is toolbox - we are not supposed to create instances of it
053: private MOFUtils() {
054: }
055:
056: /** Returns map of all Reference objects describing possible references of the given object */
057: public static Map getAllReferences(RefObject pModelElement) {
058: TypeInformation lTypeInformation = getTypeInformation(pModelElement);
059: return lTypeInformation.mAllReferences;
060: }
061:
062: // Helper. Returns cached type information
063: private static TypeInformation getTypeInformation(
064: RefObject pModelElement) {
065: MofClass lMetaObject = (MofClass) pModelElement.refMetaObject();
066: TypeInformation lTypeInformation = (TypeInformation) sTypeInfoMap
067: .get(lMetaObject);
068: if (lTypeInformation == null) {
069: lTypeInformation = new TypeInformation();
070: lTypeInformation.mTypeName = (String) lMetaObject
071: .refGetValue("name");
072: lTypeInformation.mParentReferences = new ArrayList();
073: lTypeInformation.mContainedCollectionsReferences = new ArrayList();
074: lTypeInformation.mContainedReferences = new ArrayList();
075: Map lAllReferences = new HashMap();
076: // Find out the reference name in parent and multiplicity of the association if multiplicty is one - we do not need instance name
077: // Collect contents for the type and all supertypes
078: List lAllContents = new ArrayList();
079: lAllContents.addAll(lMetaObject.getContents());
080: for (Iterator lSupertypesIterator = lMetaObject
081: .allSupertypes().iterator(); lSupertypesIterator
082: .hasNext();)
083: lAllContents.addAll(((MofClass) lSupertypesIterator
084: .next()).getContents());
085: for (Iterator lAllContentsIterator = lAllContents
086: .iterator(); lAllContentsIterator.hasNext();) {
087: ModelElement lFeature = (ModelElement) lAllContentsIterator
088: .next();
089: if (lFeature instanceof Reference) {
090: Reference lReference = (Reference) lFeature;
091: String lReferenceName = lReference.getName();
092: lAllReferences.put(lReferenceName, lReference);
093: AssociationEnd lReferencedEnd = lReference
094: .getReferencedEnd();
095: if (lReferencedEnd.getAggregation().equals(
096: AggregationKindEnum.COMPOSITE)) {
097: lTypeInformation.mParentReferences
098: .add(lReference);
099: } else {
100: AssociationEnd lExposedEnd = lReference
101: .getExposedEnd();
102: if (lExposedEnd.getAggregation().equals(
103: AggregationKindEnum.COMPOSITE)) {
104: lTypeInformation.mContainedReferences
105: .add(lReference);
106: if (lReferencedEnd.getMultiplicity()
107: .getUpper() != 1)
108: lTypeInformation.mContainedCollectionsReferences
109: .add(lReference);
110: }
111: }
112: }
113: }
114: lTypeInformation.mAllReferences = Collections
115: .unmodifiableMap(lAllReferences);
116: sTypeInfoMap.put(lMetaObject, lTypeInformation);
117: }
118: return lTypeInformation;
119: }
120: }
|