001
002package tech.aroma.application.service.server;
003
004/*
005 * Copyright 2015 Aroma Tech.
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020
021
022import com.google.inject.AbstractModule;
023import com.google.inject.Guice;
024import com.google.inject.Injector;
025import com.google.inject.Provides;
026import com.google.inject.ProvisionException;
027import java.net.SocketException;
028import org.apache.thrift.TException;
029import org.apache.thrift.protocol.TBinaryProtocol;
030import org.apache.thrift.server.TThreadPoolServer;
031import org.apache.thrift.transport.TServerSocket;
032import org.apache.thrift.transport.TTransportException;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035import tech.aroma.application.service.ModuleApplicationService;
036import tech.aroma.application.service.operations.ModuleApplicationServiceOperations;
037import tech.aroma.data.cassandra.ModuleCassandraDataRepositories;
038import tech.aroma.data.cassandra.ModuleCassandraDevCluster;
039import tech.aroma.thrift.application.service.ApplicationService;
040import tech.aroma.thrift.application.service.ApplicationServiceConstants;
041import tech.aroma.thrift.authentication.service.AuthenticationService;
042import tech.aroma.thrift.notification.service.NotificationService;
043import tech.aroma.thrift.services.Clients;
044import tech.aroma.thrift.services.NoOpNotificationService;
045import tech.sirwellington.alchemy.annotations.access.Internal;
046
047import static java.util.concurrent.TimeUnit.SECONDS;
048
049/**
050 * This Main Class runs the Authentication Service on a Server Socket.
051 * 
052 * @author SirWellington
053 */
054@Internal
055public final class TcpServer
056{
057
058    private final static Logger LOG = LoggerFactory.getLogger(TcpServer.class);
059    private static final int PORT = ApplicationServiceConstants.SERVICE_PORT;
060
061    public static void main(String[] args) throws TTransportException, SocketException
062    {
063        Injector injector = Guice.createInjector(new AromaServicesProvider(),
064                                                 new ModuleApplicationServiceOperations(),
065                                                 new ModuleApplicationService(),
066                                                 new ModuleCassandraDataRepositories(),
067                                                 new ModuleCassandraDevCluster());
068
069        ApplicationService.Iface applicationService = injector.getInstance(ApplicationService.Iface.class);
070        ApplicationService.Processor processor = new ApplicationService.Processor<>(applicationService);
071
072        TServerSocket socket = new TServerSocket(PORT);
073        socket.getServerSocket().setSoTimeout((int) SECONDS.toMillis(30));
074
075        TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(socket)
076            .protocolFactory(new TBinaryProtocol.Factory())
077            .processor(processor)
078            .requestTimeout(60)
079            .requestTimeoutUnit(SECONDS)
080            .minWorkerThreads(5)
081            .maxWorkerThreads(100);
082        
083        LOG.info("Starting Application Service at port {}", PORT);
084        
085        TThreadPoolServer server = new TThreadPoolServer(serverArgs);
086        server.serve();
087        server.stop();
088    }
089    
090    private static class AromaServicesProvider extends AbstractModule
091    {
092
093        @Override
094        protected void configure()
095        {
096        }
097        
098        @Provides
099        AuthenticationService.Iface provideAuthenticationService()
100        {
101            try
102            {
103                return Clients.newPerRequestAuthenticationServiceClient();
104            }
105            catch (TException ex)
106            {
107                throw new ProvisionException("Could not create Aroma Service Client", ex);
108            }
109        }
110        
111        @Provides
112        NotificationService.Iface provideNotificationService()
113        {
114            return new NoOpNotificationService();
115        }
116        
117    }
118}