KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > StorableFeatured


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.storagemodel;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.jmi.model.*;
25 import javax.jmi.reflect.*;
26
27 import org.netbeans.mdr.persistence.*;
28 import org.netbeans.mdr.util.*;
29 import org.netbeans.mdr.handlers.BaseObjectHandler;
30
31 /** Superclass for repository objects that can have features (class proxies, instances)
32  *
33  * @author Martin Matula, Petr Hrebejk
34  * @version 0.3
35  */

36 public abstract class StorableFeatured extends StorableBaseObject implements Streamable {
37     /** Creates new StorableFeatured providing all needed arguments.
38      * @param mdrStorage parent MdrStorage
39      * @param immediatePackage MOFID of immediate package
40      * @param meta MOFID of metaobject
41      * @throws StorageException problem in storage
42      */

43     public StorableFeatured(MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta) throws StorageException {
44         this (mdrStorage, immediatePackage, meta, null);
45     }
46     
47     public StorableFeatured (MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta, String JavaDoc storageId) throws StorageException {
48         super (mdrStorage, immediatePackage, meta, storageId);
49     }
50
51     /** Creates new StorableFeatured.
52      * (This default constructor is used only during deserialization of repository
53      * object and is immediately followed by call to {@link read} method.)
54      */

55     public StorableFeatured() {
56         super();
57     }
58     
59     protected void checkValue(StorableClass.AttributeDescriptor desc, Object JavaDoc value) {
60         if (desc.getMofId() == null) return;
61         if (desc.isMultivalued() || !desc.isChangeable()) {
62             throw new InvalidCallException(getMdrStorage().getRepository().getHandler(this), getMetaElement(desc.getMofId()));
63         } else {
64             if (value == null) {
65                 if (desc.getMinSize() > 0) {
66                     throw new NullPointerException JavaDoc();
67                 }
68             } else {
69                 if (!desc.getType().isInstance(value)) {
70                     throw new TypeMismatchException(desc.getType(), value, getMetaElement(desc.getMofId()));
71                 }
72             }
73         }
74     }
75     
76     private void checkInitValue(StorableClass.AttributeDescriptor desc, Object JavaDoc value) {
77         if (desc.getMofId() == null) return;
78         if (desc.isMultivalued()) {
79             throw new InvalidCallException(getMdrStorage().getRepository().getHandler(this), getMetaElement(desc.getMofId()));
80         }
81         if ((value != null) && !desc.getType().isInstance(value)) {
82             throw new TypeMismatchException(desc.getType(), value, getMetaElement(desc.getMofId()));
83         }
84     }
85     
86     protected Object JavaDoc getInitialValue(StorableClass.AttributeDescriptor desc, Object JavaDoc value) throws StorageException {
87         Object JavaDoc result;
88         if (desc.isMultivalued()) {
89             if (desc.isChangeable()) {
90                 if (desc.isUnique()) {
91                     result = desc.isOrdered() ? (Collection) new AttrUList(this, desc, (List) value) : (Collection) new AttrSet(this, desc, (Collection) value);
92                 } else {
93                     result = desc.isOrdered() ? (Collection) new AttrList(this, desc, (List) value) : (Collection) new AttrCollection(this, desc, (Collection) value);
94                 }
95             } else {
96                 result = desc.isUnique() ? (List) new AttrImmutUList(this, desc, (Collection) value) : (List) new AttrImmutList(this, desc, (Collection) value);
97             }
98         } else {
99             try {
100                 checkInitValue (desc, value);
101             } catch (RuntimeException JavaDoc e) {
102                 Logger.getDefault().annotate(e, "multivalued: " + desc.isMultivalued() + ", name: " + desc.getName());
103                 Logger.getDefault().annotate(e, "maxSize: " + desc.getMaxSize() + ", minSize: " + desc.getMinSize());
104                 throw e;
105             }
106             result = value;
107             if (result instanceof RefObject) {
108                 StorableObject storableObj = (StorableObject) ((BaseObjectHandler) result)._getDelegate();
109                 storableObj.setComposite(this, storableObj.getMofId(), desc.getMofId());
110             }
111         } // else
112
return result;
113     }
114     
115     private RefObject getMetaElement(org.netbeans.mdr.persistence.MOFID mofId) {
116         try {
117             return (RefObject) getMdrStorage().getRepository().getHandler(getMdrStorage().getObject(mofId));
118         } catch (StorageException e) {
119             Logger.getDefault().notify(Logger.INFORMATIONAL, e);
120             return null;
121         }
122     }
123     
124     /**
125      * @param feature MOFID of attribute metaobject
126      * @param value new value of the specified attribute
127      * @throws StorageException problem in storage
128      */

129     public abstract void setAttribute(String JavaDoc featureName, Object JavaDoc value) throws StorageException;
130     
131     public abstract Object JavaDoc getAttribute(String JavaDoc featureName) throws StorageException;
132     
133     public abstract StorableClass getClassProxy() throws StorageException;
134 }
135
Popular Tags