001    package org.apache.ojb.otm.copy;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.PersistenceBroker;
019    
020    
021    /**
022     * @author matthew.baird
023     */
024    public class CloneableObjectCopyStrategy implements ObjectCopyStrategy
025    {
026            /**
027             * If users want to implement clone on all their objects, we can use this
028             * to make copies. This is hazardous as user may mess it up, but it is also
029             * potentially the fastest way of making a copy.
030             *
031             * Usually the OjbCloneable interface should just be delegating to the clone()
032             * operation that the user has implemented.
033             *
034             * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object)
035             *
036             */
037            public Object copy(Object obj, PersistenceBroker broker)
038                            throws ObjectCopyException
039            {
040                    if (obj instanceof OjbCloneable)
041                    {
042                            try
043                            {
044                                    return ((OjbCloneable) obj).ojbClone();
045                            }
046                            catch (Exception e)
047                            {
048                                    throw new ObjectCopyException(e);
049                            }
050                    }
051                    else
052                    {
053                            throw new ObjectCopyException("Object must implement OjbCloneable in order to use the"
054                                                                                      + " CloneableObjectCopyStrategy");
055                    }
056            }
057    }