001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.schema;
021    
022    
023    import javax.naming.NamingException;
024    
025    import org.apache.directory.shared.ldap.schema.registries.Registries;
026    import org.apache.directory.shared.ldap.util.StringTools;
027    
028    
029    /**
030     * An abstract class used to manage the ADS specific SchemaObject, which can
031     * contain some compiled Java class to implement the specific logic.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 437007 $
035     */
036    public abstract class LoadableSchemaObject extends AbstractSchemaObject
037    {
038        /** The serialVersionUID */
039        private static final long serialVersionUID = 1L;
040    
041        /** The Full Qualified Class Name */
042        private String            fqcn;
043    
044        /** The base64 encoded bytecode for this schema */
045        private String            bytecode;
046    
047    
048        /**
049         * Constructor to use when the OID is known in advance.
050         * 
051         * @param objectType The SchemaObject type
052         * @param oid The SchemaObject OID
053         */
054        protected LoadableSchemaObject( SchemaObjectType objectType, String oid )
055        {
056            super( objectType, oid );
057    
058            fqcn = "";
059            bytecode = null;
060        }
061    
062    
063        /**
064         * Constructor to use when the OID is not known until after instantiation.
065         * 
066         * @param objectType The SchemaObject type
067         */
068        protected LoadableSchemaObject( SchemaObjectType objectType )
069        {
070            super( objectType );
071    
072            fqcn = "";
073            bytecode = null;
074        }
075    
076    
077        /**
078         * @return The associated bytecode of this SchemaObject instance
079         */
080        public String getBytecode()
081        {
082            return bytecode;
083        }
084    
085    
086        /**
087         * Stores some bytecode representing the compiled Java class for this
088         * SchemaObject instance.
089         * 
090         * @param bytecode The bytecode to store
091         */
092        public void setBytecode( String bytecode )
093        {
094            if ( !isReadOnly )
095            {
096                this.bytecode = bytecode;
097            }
098        }
099    
100    
101        /**
102         * @return The chemaObject instance Fully Qualified Class Name
103         */
104        public String getFqcn()
105        {
106            return fqcn;
107        }
108    
109    
110        /**
111         * Set the Fully Qualified Class Name for this SchemaObject instance
112         * class stored in the bytecode attribute
113         * @param fqcn The Fully Qualified Class Name
114         */
115        public void setFqcn( String fqcn )
116        {
117            if ( !isReadOnly )
118            {
119                this.fqcn = fqcn;
120            }
121        }
122    
123    
124        /**
125         * {@inheritDoc}
126         */
127        public void registerOid( SchemaObject schemaObject, Registries registries ) throws NamingException
128        {
129            // Do nothing : the current SchemaObject ha sthe same OID than the one it is realted to
130        }
131    
132    
133        /**
134         * {@inheritDoc}
135         */
136        public LoadableSchemaObject copy()
137        {
138            return null;
139        }
140    
141    
142        /**
143         * @see Object#equals()
144         */
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            // TODO
161    
162            // Check the FQCN
163            if ( fqcn == null )
164            {
165                return that.fqcn == null;
166            }
167            else
168            {
169                return fqcn.equals( that.fqcn );
170            }
171        }
172    
173    
174        /**
175         * Test that the FQCN is equal to the instance's name. If the FQCN is
176         * empty, fill it with the instance's name
177         *
178         * @return true if the FQCN is correctly set
179         */
180        public boolean isValid()
181        {
182            String className = this.getClass().getName();
183    
184            if ( StringTools.isEmpty( fqcn ) )
185            {
186                fqcn = className;
187                return true;
188            }
189            else
190            {
191                return className.equals( fqcn );
192            }
193        }
194    }