View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.model.schema;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.util.Strings;
25  
26  
27  /**
28   * An abstract class used to manage the ADS specific SchemaObject, which can
29   * contain some compiled Java class to implement the specific logic.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public abstract class LoadableSchemaObject extends AbstractSchemaObject
34  {
35      /** The serial version UID */
36      private static final long serialVersionUID = 2L;
37  
38      /** The Full Qualified Class Name */
39      private String fqcn;
40  
41      /** The base64 encoded bytecode for this schema */
42      private String bytecode;
43  
44  
45      /**
46       * Constructor to use when the OID is known in advance.
47       * 
48       * @param objectType The SchemaObject type
49       * @param oid The SchemaObject OID
50       */
51      protected LoadableSchemaObject( SchemaObjectType objectType, String oid )
52      {
53          super( objectType, oid );
54  
55          fqcn = "";
56          bytecode = null;
57      }
58  
59  
60      /**
61       * Constructor to use when the OID is not known until after instantiation.
62       * 
63       * @param objectType The SchemaObject type
64       */
65      protected LoadableSchemaObject( SchemaObjectType objectType )
66      {
67          super( objectType );
68  
69          fqcn = "";
70          bytecode = null;
71      }
72  
73  
74      /**
75       * @return The associated bytecode of this SchemaObject instance
76       */
77      public String getBytecode()
78      {
79          return bytecode;
80      }
81  
82  
83      /**
84       * Stores some bytecode representing the compiled Java class for this
85       * SchemaObject instance.
86       * 
87       * @param bytecode The bytecode to store
88       */
89      public void setBytecode( String bytecode )
90      {
91          if ( locked )
92          {
93              throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
94          }
95  
96          if ( !isReadOnly )
97          {
98              this.bytecode = bytecode;
99          }
100     }
101 
102 
103     /**
104      * @return The chemaObject instance Fully Qualified Class Name
105      */
106     public String getFqcn()
107     {
108         return fqcn;
109     }
110 
111 
112     /**
113      * Set the Fully Qualified Class Name for this SchemaObject instance
114      * class stored in the bytecode attribute
115      * @param fqcn The Fully Qualified Class Name
116      */
117     public void setFqcn( String fqcn )
118     {
119         if ( locked )
120         {
121             throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
122         }
123 
124         if ( !isReadOnly )
125         {
126             this.fqcn = fqcn;
127         }
128     }
129 
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public LoadableSchemaObject copy()
136     {
137         return null;
138     }
139 
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public boolean equals( Object o )
146     {
147         if ( !super.equals( o ) )
148         {
149             return false;
150         }
151 
152         if ( !( o instanceof LoadableSchemaObject ) )
153         {
154             return false;
155         }
156 
157         LoadableSchemaObject that = ( LoadableSchemaObject ) o;
158 
159         // Check the byteCode
160 
161         // Check the FQCN
162         if ( fqcn == null )
163         {
164             return that.fqcn == null;
165         }
166         else
167         {
168             return fqcn.equals( that.fqcn );
169         }
170     }
171 
172 
173     /**
174      * Test that the FQCN is equal to the instance's name. If the FQCN is
175      * empty, fill it with the instance's name
176      *
177      * @return true if the FQCN is correctly set
178      */
179     public boolean isValid()
180     {
181         String className = this.getClass().getName();
182 
183         if ( Strings.isEmpty( fqcn ) )
184         {
185             fqcn = className;
186             return true;
187         }
188         else
189         {
190             return className.equals( fqcn );
191         }
192     }
193 }