001    package org.apache.ojb.broker.metadata;
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 java.io.Serializable;
019    import java.util.Properties;
020    
021    import org.apache.commons.lang.SystemUtils;
022    import org.apache.commons.lang.builder.ToStringBuilder;
023    import org.apache.commons.lang.builder.ToStringStyle;
024    import org.apache.ojb.broker.util.XmlHelper;
025    
026    /**
027     * Encapsulates sequence manager configuration properties managed by
028     * {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}.
029     * <br/>
030     * All sequence manager implementation specific configuration
031     * attributes are represented by key/value pairs in a
032     * <code>Properties</code> object and could be reached via
033     * {@link #getConfigurationProperties} or {@link #getAttribute(String key)}.
034     *
035     * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
036     * @version $Id: SequenceDescriptor.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
037     */
038    public class SequenceDescriptor implements Serializable, XmlCapable, AttributeContainer
039    {
040    
041            private static final long serialVersionUID = -5161713731380949398L;
042        private JdbcConnectionDescriptor jcd;
043        private Class sequenceManagerClass;
044        private Properties configurationProperties;
045    
046        public SequenceDescriptor(JdbcConnectionDescriptor jcd)
047        {
048            this.jcd = jcd;
049            this.configurationProperties = new Properties();
050        }
051    
052        public SequenceDescriptor(JdbcConnectionDescriptor jcd, Class sequenceManagerClass)
053        {
054            this(jcd);
055            this.sequenceManagerClass = sequenceManagerClass;
056        }
057    
058        public JdbcConnectionDescriptor getJdbcConnectionDescriptor()
059        {
060            return jcd;
061        }
062    
063        public void setJdbcConnectionDescriptor(JdbcConnectionDescriptor jcd)
064        {
065            this.jcd = jcd;
066        }
067    
068        public Class getSequenceManagerClass()
069        {
070            return sequenceManagerClass;
071        }
072    
073        public void setSequenceManagerClass(Class sequenceManagerClass)
074        {
075            this.sequenceManagerClass = sequenceManagerClass;
076        }
077    
078        public void addAttribute(String attributeName, String attributeValue)
079        {
080            configurationProperties.setProperty(attributeName, attributeValue);
081        }
082    
083        public String getAttribute(String key)
084        {
085            return getAttribute(key, null);
086        }
087    
088        public String getAttribute(String attributeName, String defaultValue)
089        {
090            String result = configurationProperties.getProperty(attributeName);
091            if(result == null) result = defaultValue;
092            return result;
093        }
094    
095        public Properties getConfigurationProperties()
096        {
097            return configurationProperties;
098        }
099    
100        public void setConfigurationProperties(Properties configurationProperties)
101        {
102            this.configurationProperties = configurationProperties;
103        }
104    
105        public String toString()
106        {
107            ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
108            buf.append("   sequenceManagerClass", getSequenceManagerClass()).
109            append("   Properties", getConfigurationProperties());
110            return buf.toString();
111        }
112    
113        public String toXML()
114        {
115            RepositoryTags tags = RepositoryTags.getInstance();
116            String eol = SystemUtils.LINE_SEPARATOR;
117            StringBuffer buf = new StringBuffer( 1024 );
118            //opening tag + attributes
119            buf.append( "      " );
120            buf.append( tags.getOpeningTagNonClosingById( SEQUENCE_MANAGER ) );
121            buf.append( eol );
122            buf.append( "         " );
123            buf.append( tags.getAttribute( SEQUENCE_MANAGER_CLASS, "" + getSequenceManagerClass().getName() ) );
124            buf.append( "      >" );
125            buf.append( eol );
126            buf.append( "         <!-- " );
127            buf.append( eol );
128            buf.append( "         Add sequence manger properties here, using custom attributes" );
129            buf.append( eol );
130            buf.append( "         e.g. <attribute attribute-name=\"grabSize\" attribute-value=\"20\"/>" );
131            buf.append( eol );
132            buf.append( "         -->" );
133            buf.append( eol );
134            XmlHelper.appendSerializedAttributes(buf, "         ", getConfigurationProperties());
135            buf.append( "      " );
136            buf.append( tags.getClosingTagById( SEQUENCE_MANAGER ) );
137            buf.append( eol );
138    
139            return buf.toString();
140        }
141    
142    }