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 */ 019package org.apache.directory.server.protocol.shared; 020 021 022import java.util.HashSet; 023import java.util.Set; 024 025import org.apache.directory.server.protocol.shared.transport.Transport; 026import org.apache.mina.transport.socket.DatagramAcceptor; 027import org.apache.mina.transport.socket.SocketAcceptor; 028 029 030/** 031 * An abstract base class for a ProtocolService. The start/stop methods have 032 * not been implemented. 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public abstract class AbstractProtocolService implements ProtocolService 037{ 038 /** A flag set to indicate if the server is started or not */ 039 private boolean started; 040 041 /** A flag set to tell if the server is enabled or not */ 042 private boolean enabled; 043 044 /** The server ID */ 045 private String serviceId; 046 047 /** The service name */ 048 private String serviceName; 049 050 /** The service transports. We may have more than one */ 051 protected Set<Transport> transports = new HashSet<>(); 052 053 054 /** 055 * {@inheritDoc} 056 */ 057 public boolean isStarted() 058 { 059 return started; 060 } 061 062 063 /** 064 * @param started The state of this server 065 */ 066 protected void setStarted( boolean started ) 067 { 068 this.started = started; 069 } 070 071 072 /** 073 * {@inheritDoc} 074 */ 075 public boolean isEnabled() 076 { 077 return enabled; 078 } 079 080 081 /** 082 * {@inheritDoc} 083 */ 084 public void setEnabled( boolean enabled ) 085 { 086 this.enabled = enabled; 087 } 088 089 090 /** 091 * {@inheritDoc} 092 */ 093 public String getServiceId() 094 { 095 return serviceId; 096 } 097 098 099 /** 100 * {@inheritDoc} 101 */ 102 public void setServiceId( String serviceId ) 103 { 104 this.serviceId = serviceId; 105 } 106 107 108 /** 109 * {@inheritDoc} 110 */ 111 public String getServiceName() 112 { 113 return serviceName; 114 } 115 116 117 /** 118 * {@inheritDoc} 119 */ 120 public void setServiceName( String name ) 121 { 122 this.serviceName = name; 123 } 124 125 126 /** 127 * @return the transport 128 */ 129 public Transport[] getTransports() 130 { 131 return transports.toArray( new Transport[] 132 {} ); 133 } 134 135 136 /** 137 * Set the underlying transports 138 * @param transports The transports 139 */ 140 public void setTransports( Transport... transports ) 141 { 142 for ( Transport transport : transports ) 143 { 144 this.transports.add( transport ); 145 146 if ( transport.getAcceptor() == null ) 147 { 148 transport.init(); 149 } 150 } 151 } 152 153 154 /** 155 * Add underlying transports 156 * @param transports The transports 157 */ 158 public void addTransports( Transport... transports ) 159 { 160 for ( Transport transport : transports ) 161 { 162 this.transports.add( transport ); 163 164 if ( transport.getAcceptor() == null ) 165 { 166 transport.init(); 167 } 168 } 169 } 170 171 172 /** 173 * {@inheritDoc} 174 */ 175 public DatagramAcceptor getDatagramAcceptor( Transport udpTransport ) 176 { 177 return ( DatagramAcceptor ) udpTransport.getAcceptor(); 178 } 179 180 181 /** 182 * {@inheritDoc} 183 */ 184 public SocketAcceptor getSocketAcceptor( Transport tcpTransport ) 185 { 186 return ( SocketAcceptor ) tcpTransport.getAcceptor(); 187 } 188}