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 */
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_04441, getName() ) );
094        }
095
096        if ( !isReadOnly )
097        {
098            this.bytecode = bytecode;
099        }
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    public LoadableSchemaObject copy()
135    {
136        return null;
137    }
138
139
140    /**
141     * {@inheritDoc}
142     */
143    @Override
144    public boolean equals( Object o )
145    {
146        if ( !super.equals( o ) )
147        {
148            return false;
149        }
150
151        if ( !( o instanceof LoadableSchemaObject ) )
152        {
153            return false;
154        }
155
156        LoadableSchemaObject that = ( LoadableSchemaObject ) o;
157
158        // Check the byteCode
159        // TODO
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}