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.studio.apacheds.model;
021    
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.UUID;
026    
027    import org.apache.directory.studio.apacheds.jobs.LaunchServerJob;
028    import org.eclipse.core.runtime.IAdaptable;
029    
030    
031    /**
032     * This class represents an Apache DS server.
033     *
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class Server implements IAdaptable
038    {
039        /** The name of the server */
040        private String name;
041    
042        /** The ID of the server */
043        private String id;
044    
045        /** The version of the server */
046        private ServerVersion version = ServerVersion.VERSION_1_5_5;
047    
048        /** The state of the server */
049        private ServerStateEnum state = ServerStateEnum.STOPPED;
050    
051        /** The listeners list*/
052        private List<ServerListener> listeners = new ArrayList<ServerListener>();
053    
054        /** The launch job */
055        private LaunchServerJob launchJob;
056    
057    
058        /**
059         * Creates a new instance of Server.
060         */
061        public Server()
062        {
063        }
064    
065    
066        /**
067         * Adds a listener.
068         *
069         * @param listener
070         *      the listener
071         */
072        public void addListener( ServerListener listener )
073        {
074            if ( listener == null )
075            {
076                throw new IllegalArgumentException( Messages.getString( "Server.ListenerCantBeNull" ) ); //$NON-NLS-1$
077            }
078    
079            listeners.add( listener );
080        }
081    
082    
083        /**
084         * Removes a listener.
085         *
086         * @param listener
087         *      the listener
088         */
089        public void removeListener( ServerListener listener )
090        {
091            if ( listener == null )
092            {
093                throw new IllegalArgumentException( Messages.getString( "Server.ListenerCantBeNull" ) ); //$NON-NLS-1$
094            }
095    
096            listeners.remove( listener );
097        }
098    
099    
100        /**
101         * Creates a new instance of Server.
102         * <p>
103         * An ID is automatically created.
104         *
105         * @param name
106         *      the name of the server
107         */
108        public Server( String name )
109        {
110            this.name = name;
111            id = createId();
112        }
113    
114    
115        /**
116         * Creates a new instance of Server.
117         *
118         * @param name
119         *      the name of the server
120         * @param id
121         *      the id of the server
122         */
123        public Server( String name, String id )
124        {
125            this.name = name;
126            this.id = id;
127        }
128    
129    
130        /**
131         * Gets the name of the server
132         *
133         * @return
134         *      the name of the server
135         */
136        public String getName()
137        {
138            return name;
139        }
140    
141    
142        /**
143         * Sets the name of the server
144         *
145         * @param name
146         *      the name of the server
147         */
148        public void setName( String name )
149        {
150            if ( this.name == name )
151            {
152                return;
153            }
154    
155            this.name = name;
156    
157            fireServerNameChangeEvent();
158        }
159    
160    
161        /**
162         * Fire a server listener name change event.
163         */
164        private void fireServerNameChangeEvent()
165        {
166            for ( ServerListener listener : listeners.toArray( new ServerListener[0] ) )
167            {
168                listener.serverChanged( new ServerEvent( this, ServerEventEnum.RENAMED ) );
169            }
170        }
171    
172    
173        /**
174         * Gets the ID of the server.
175         *
176         * @return
177         *      the ID of the server
178         */
179        public String getId()
180        {
181            return id;
182        }
183    
184    
185        /**
186         * Sets the ID of the server.
187         *
188         * @param id
189         *      the ID of the server
190         */
191        public void setId( String id )
192        {
193            this.id = id;
194        }
195    
196    
197        /**
198         * Creates a new ID.
199         *
200         * @return
201         *      a new ID
202         */
203        public static String createId()
204        {
205            return UUID.randomUUID().toString();
206        }
207    
208    
209        /**
210         * Gets the state.
211         *
212         * @return
213         *      the state
214         */
215        public ServerStateEnum getState()
216        {
217            return state;
218        }
219    
220    
221        /**
222         * Sets the state
223         *
224         * @param state
225         *      the state
226         */
227        public void setState( ServerStateEnum state )
228        {
229            if ( this.state == state )
230            {
231                return;
232            }
233    
234            this.state = state;
235    
236            fireServerStateChangeEvent();
237        }
238    
239    
240        /**
241         * Fires a server listener state change event.
242         */
243        private void fireServerStateChangeEvent()
244        {
245            for ( ServerListener listener : listeners.toArray( new ServerListener[0] ) )
246            {
247                listener.serverChanged( new ServerEvent( this, ServerEventEnum.STATE_CHANGED ) );
248            }
249        }
250    
251    
252        /**
253         * Gets the launch job.
254         *
255         * @return
256         *      the launch job
257         */
258        public LaunchServerJob getLaunchJob()
259        {
260            return launchJob;
261        }
262    
263    
264        /**
265         * Sets the launch job.
266         *
267         * @param launchJob
268         *      the launch job
269         */
270        public void setLaunchJob( LaunchServerJob launchJob )
271        {
272            this.launchJob = launchJob;
273        }
274    
275    
276        /**
277         * Gets the version of the server
278         *
279         * @return
280         *      the version of the server
281         */
282        public ServerVersion getVersion()
283        {
284            return version;
285        }
286    
287    
288        /**
289         * Sets the version of the server
290         *
291         * @param version
292         *      the version of the server
293         */
294        public void setVersion( ServerVersion version )
295        {
296            this.version = version;
297        }
298    
299    
300        /* (non-Javadoc)
301         * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
302         */
303        public Object getAdapter( Class adapter )
304        {
305            return null;
306        }
307    }