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 org.apache.directory.shared.i18n.I18n;
024    import org.apache.directory.shared.ldap.exception.LdapException;
025    
026    import org.apache.directory.shared.ldap.schema.registries.Registries;
027    import org.apache.directory.shared.ldap.util.StringTools;
028    
029    
030    /**
031     * An abstract class used to manage the ADS specific SchemaObject, which can
032     * contain some compiled Java class to implement the specific logic.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev: 437007 $
036     */
037    public abstract class LoadableSchemaObject extends AbstractSchemaObject
038    {
039        /** The serialVersionUID */
040        private static final long serialVersionUID = 1L;
041    
042        /** The Full Qualified Class Name */
043        private String            fqcn;
044    
045        /** The base64 encoded bytecode for this schema */
046        private String            bytecode;
047    
048    
049        /**
050         * Constructor to use when the OID is known in advance.
051         * 
052         * @param objectType The SchemaObject type
053         * @param oid The SchemaObject OID
054         */
055        protected LoadableSchemaObject( SchemaObjectType objectType, String oid )
056        {
057            super( objectType, oid );
058    
059            fqcn = "";
060            bytecode = null;
061        }
062    
063    
064        /**
065         * Constructor to use when the OID is not known until after instantiation.
066         * 
067         * @param objectType The SchemaObject type
068         */
069        protected LoadableSchemaObject( SchemaObjectType objectType )
070        {
071            super( objectType );
072    
073            fqcn = "";
074            bytecode = null;
075        }
076    
077    
078        /**
079         * @return The associated bytecode of this SchemaObject instance
080         */
081        public String getBytecode()
082        {
083            return bytecode;
084        }
085    
086    
087        /**
088         * Stores some bytecode representing the compiled Java class for this
089         * SchemaObject instance.
090         * 
091         * @param bytecode The bytecode to store
092         */
093        public void setBytecode( String bytecode )
094        {
095            if ( locked )
096            {
097                throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
098            }
099            
100            if ( !isReadOnly )
101            {
102                this.bytecode = bytecode;
103            }
104        }
105    
106    
107        /**
108         * @return The chemaObject instance Fully Qualified Class Name
109         */
110        public String getFqcn()
111        {
112            return fqcn;
113        }
114    
115    
116        /**
117         * Set the Fully Qualified Class Name for this SchemaObject instance
118         * class stored in the bytecode attribute
119         * @param fqcn The Fully Qualified Class Name
120         */
121        public void setFqcn( String fqcn )
122        {
123            if ( locked )
124            {
125                throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
126            }
127            
128            if ( !isReadOnly )
129            {
130                this.fqcn = fqcn;
131            }
132        }
133    
134    
135        /**
136         * {@inheritDoc}
137         */
138        public void registerOid( SchemaObject schemaObject, Registries registries ) throws LdapException
139        {
140            // Do nothing : the current SchemaObject ha sthe same OID than the one it is realted to
141        }
142    
143    
144        /**
145         * {@inheritDoc}
146         */
147        public LoadableSchemaObject copy()
148        {
149            return null;
150        }
151    
152    
153        /**
154         * @see Object#equals()
155         */
156        public boolean equals( Object o )
157        {
158            if ( !super.equals( o ) )
159            {
160                return false;
161            }
162    
163            if ( !( o instanceof LoadableSchemaObject ) )
164            {
165                return false;
166            }
167    
168            LoadableSchemaObject that = ( LoadableSchemaObject ) o;
169    
170            // Check the byteCode
171            // TODO
172    
173            // Check the FQCN
174            if ( fqcn == null )
175            {
176                return that.fqcn == null;
177            }
178            else
179            {
180                return fqcn.equals( that.fqcn );
181            }
182        }
183    
184    
185        /**
186         * Test that the FQCN is equal to the instance's name. If the FQCN is
187         * empty, fill it with the instance's name
188         *
189         * @return true if the FQCN is correctly set
190         */
191        public boolean isValid()
192        {
193            String className = this.getClass().getName();
194    
195            if ( StringTools.isEmpty( fqcn ) )
196            {
197                fqcn = className;
198                return true;
199            }
200            else
201            {
202                return className.equals( fqcn );
203            }
204        }
205    }