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.shared.ldap.extras.extended.ads_impl.gracefulShutdown;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
025import org.apache.directory.shared.ldap.codec.api.ExtendedRequestFactory;
026import org.apache.directory.shared.ldap.codec.api.ExtendedResponseDecorator;
027import org.apache.directory.shared.ldap.codec.api.LdapApiService;
028import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownRequestImpl;
029import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownResponseImpl;
030import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownRequest;
031import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownResponse;
032import org.apache.directory.shared.ldap.model.message.ExtendedRequest;
033import org.apache.directory.shared.ldap.model.message.ExtendedResponse;
034
035
036/**
037 * An {@link ExtendedRequestFactory} for creating cancel extended request response 
038 * pairs.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class GracefulShutdownFactory 
043    implements ExtendedRequestFactory<GracefulShutdownRequest, GracefulShutdownResponse>
044{
045    private LdapApiService codec;
046    
047    
048    public GracefulShutdownFactory( LdapApiService codec )
049    {
050        this.codec = codec;
051    }
052    
053    
054    /**
055     * {@inheritDoc}
056     */
057    public String getOid()
058    {
059        return GracefulShutdownRequest.EXTENSION_OID;
060    }
061
062    
063    /**
064     * {@inheritDoc}
065     */
066    public GracefulShutdownRequest newRequest()
067    {
068        return new GracefulShutdownRequestDecorator( codec, new GracefulShutdownRequestImpl() );
069    }
070
071
072    /**
073     * {@inheritDoc}
074     */
075    public GracefulShutdownResponse newResponse( byte[] encodedValue ) throws DecoderException
076    {
077        GracefulShutdownResponseDecorator response = new GracefulShutdownResponseDecorator( 
078            codec, new GracefulShutdownResponseImpl() );
079        response.setResponseValue( encodedValue );
080        return response;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    public GracefulShutdownRequest newRequest( byte[] value )
088    {
089        GracefulShutdownRequestDecorator req = new GracefulShutdownRequestDecorator( codec, new GracefulShutdownRequestImpl() );
090        req.setRequestValue( value );
091        return req;
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    public ExtendedRequestDecorator<GracefulShutdownRequest, GracefulShutdownResponse> decorate(
099        ExtendedRequest<?> modelRequest )
100    {
101        if ( modelRequest instanceof GracefulShutdownRequestDecorator )
102        {
103            return ( GracefulShutdownRequestDecorator ) modelRequest;
104        }
105
106        return new GracefulShutdownRequestDecorator( codec, ( GracefulShutdownRequest ) modelRequest );
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    public ExtendedResponseDecorator<GracefulShutdownResponse> decorate( ExtendedResponse decoratedMessage )
114    {
115        if ( decoratedMessage instanceof GracefulShutdownResponseDecorator )
116        {
117            return ( GracefulShutdownResponseDecorator ) decoratedMessage;
118        }
119
120        return new GracefulShutdownResponseDecorator( codec, ( GracefulShutdownResponse ) decoratedMessage );
121    }
122}