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 *    https://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 */
020package org.apache.directory.api.ldap.model.schema;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.util.Strings;
025
026
027/**
028 * An abstract class used to manage the ADS specific SchemaObject, which can
029 * contain some compiled Java class to implement the specific logic.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public abstract class LoadableSchemaObject extends AbstractSchemaObject
034{
035    /** The serial version UID */
036    private static final long serialVersionUID = 2L;
037
038    /** The Full Qualified Class Name */
039    private String fqcn;
040
041    /** The base64 encoded bytecode for this schema */
042    private String bytecode;
043
044
045    /**
046     * Constructor to use when the OID is known in advance.
047     * 
048     * @param objectType The SchemaObject type
049     * @param oid The SchemaObject OID
050     */
051    protected LoadableSchemaObject( SchemaObjectType objectType, String oid )
052    {
053        super( objectType, oid );
054
055        fqcn = "";
056        bytecode = null;
057    }
058
059
060    /**
061     * Constructor to use when the OID is not known until after instantiation.
062     * 
063     * @param objectType The SchemaObject type
064     */
065    protected LoadableSchemaObject( SchemaObjectType objectType )
066    {
067        super( objectType );
068
069        fqcn = "";
070        bytecode = null;
071    }
072
073
074    /**
075     * @return The associated bytecode of this SchemaObject instance
076     */
077    public String getBytecode()
078    {
079        return bytecode;
080    }
081
082
083    /**
084     * Stores some bytecode representing the compiled Java class for this
085     * SchemaObject instance.
086     * 
087     * @param bytecode The bytecode to store
088     */
089    public void setBytecode( String bytecode )
090    {
091        if ( locked )
092        {
093            throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
094        }
095
096        this.bytecode = bytecode;
097    }
098
099
100    /**
101     * @return The chemaObject instance Fully Qualified Class Name
102     */
103    public String getFqcn()
104    {
105        return fqcn;
106    }
107
108
109    /**
110     * Set the Fully Qualified Class Name for this SchemaObject instance
111     * class stored in the bytecode attribute
112     * @param fqcn The Fully Qualified Class Name
113     */
114    public void setFqcn( String fqcn )
115    {
116        if ( locked )
117        {
118            throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
119        }
120
121        this.fqcn = fqcn;
122    }
123
124
125    /**
126     * {@inheritDoc}
127     */
128    @Override
129    public LoadableSchemaObject copy()
130    {
131        return null;
132    }
133    
134    
135    /**
136     * {@inheritDoc}
137     */
138    @Override
139    public int hashCode()
140    {
141        int hash = h;
142        
143        if ( fqcn != null )
144        {
145            hash = hash * 17 + fqcn.hashCode();
146        }
147        
148        return hash;
149    }
150
151
152    /**
153     * {@inheritDoc}
154     */
155    @Override
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 ( Strings.isEmpty( fqcn ) )
196        {
197            fqcn = className;
198            return true;
199        }
200        else
201        {
202            return className.equals( fqcn );
203        }
204    }
205}