001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of Granite Data Services.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or modify
008     *   it under the terms of the GNU Library General Public License as published by
009     *   the Free Software Foundation; either version 2 of the License, or (at your
010     *   option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful, but
013     *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     *   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015     *   for more details.
016     *
017     *   You should have received a copy of the GNU Library General Public License
018     *   along with this library; if not, see <http://www.gnu.org/licenses/>.
019     */
020    package org.granite.client.messaging.channel;
021    
022    import java.net.URI;
023    import java.net.URISyntaxException;
024    import java.util.Arrays;
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    import org.granite.client.messaging.ClientAliasRegistry;
029    import org.granite.client.messaging.transport.Transport;
030    import org.granite.client.messaging.transport.TransportException;
031    import org.granite.client.platform.Platform;
032    import org.granite.messaging.AliasRegistry;
033    import org.granite.util.ContentType;
034    
035    
036    public abstract class AbstractChannelFactory implements ChannelFactory {
037            
038            protected final ContentType contentType;
039            
040            protected Transport remotingTransport = null;
041            protected Transport messagingTransport = null;
042            protected Object context = null;
043    
044            protected Set<String> scanPackageNames = null;
045            protected AliasRegistry aliasRegistry = null;
046    
047            protected Long defaultTimeToLive = null;
048    
049            
050            protected AbstractChannelFactory(ContentType contentType) {
051                    this(contentType, null, null, null);
052            }
053    
054            protected AbstractChannelFactory(ContentType contentType, Object context) {
055                    this(contentType, context, null, null);
056            }
057    
058            protected AbstractChannelFactory(ContentType contentType, Object context, Transport remotingTransport, Transport messagingTransport) {
059                    this.contentType = contentType;
060                    this.context = context;
061                    this.remotingTransport = remotingTransport;
062                    this.messagingTransport = messagingTransport;
063            }
064    
065            public Object getContext() {
066                    return context;
067            }
068    
069            public void setContext(Object context) {
070                    this.context = context;
071            }
072    
073            public ContentType getContentType() {
074                    return contentType;
075            }
076            
077            public long getDefaultTimeToLive() {
078                    return (defaultTimeToLive != null ? defaultTimeToLive.longValue() : -1L);
079            }
080    
081            public void setDefaultTimeToLive(long defaultTimeToLive) {
082                    this.defaultTimeToLive = Long.valueOf(defaultTimeToLive);
083            }
084    
085            public void setAliasRegistry(AliasRegistry aliasRegistry) {
086                this.aliasRegistry = aliasRegistry;
087            }
088    
089            public Transport getRemotingTransport() {
090                    return remotingTransport;
091            }
092    
093            public void setRemotingTransport(Transport remotingTransport) {
094                    this.remotingTransport = remotingTransport;
095            }
096    
097            public Transport getMessagingTransport() {
098                    return messagingTransport;
099            }
100    
101            public void setMessagingTransport(Transport messagingTransport) {
102                    this.messagingTransport = messagingTransport;
103            }
104    
105            public void setScanPackageNames(String... packageNames) {
106                    if (packageNames != null)
107                            this.scanPackageNames = new HashSet<String>(Arrays.asList(packageNames));
108                    else
109                            this.scanPackageNames = null;
110            }
111    
112            public void setScanPackageNames(Set<String> packageNames) {
113                    this.scanPackageNames = packageNames;
114            }
115    
116    
117            public void start() {
118                    Platform platform = Platform.getInstance();
119                    platform.setContext(context);
120                    
121                    if (remotingTransport == null)
122                            remotingTransport = Platform.getInstance().newRemotingTransport();
123                    
124                    if (!remotingTransport.isStarted() && !remotingTransport.start())
125                            throw new TransportException("Could not start remoting transport: " + remotingTransport);
126                    
127                    if (messagingTransport == null) {
128                            messagingTransport = Platform.getInstance().newMessagingTransport();
129                            if (messagingTransport == null)
130                                    messagingTransport = remotingTransport;
131                    }
132                    else if (!messagingTransport.isStarted() && !messagingTransport.start())
133                            throw new TransportException("Could not start messaging transport: " + messagingTransport);
134                    
135                    if (aliasRegistry == null)
136                            aliasRegistry = new ClientAliasRegistry();
137                    
138                    if (scanPackageNames != null)
139                            aliasRegistry.scan(scanPackageNames);
140            }
141            
142            public void stop() {
143                    aliasRegistry = null;
144                    
145                    stop(true);
146            }
147    
148            public void stop(boolean stopTransports) {
149                    if (stopTransports) {
150                            if (remotingTransport != null && remotingTransport.isStarted()) {
151                                    remotingTransport.stop();
152                                    remotingTransport = null;
153                            }
154                            
155                            if (messagingTransport != null && messagingTransport.isStarted()) {
156                                    messagingTransport.stop();
157                                    messagingTransport = null;
158                            }
159                    }
160            }
161    
162            @Override
163            public RemotingChannel newRemotingChannel(String id, String uri) {
164                    try {
165                            RemotingChannel channel = newRemotingChannel(id, new URI(uri));
166                            if (defaultTimeToLive != null)
167                                    channel.setDefaultTimeToLive(defaultTimeToLive);
168                            return channel;
169                    }
170                    catch (URISyntaxException e) {
171                            throw new IllegalArgumentException("Bad uri: " + uri, e);
172                    }
173            }
174    
175            @Override
176            public RemotingChannel newRemotingChannel(String id, String uri, int maxConcurrentRequests) {
177                    try {
178                            RemotingChannel channel = newRemotingChannel(id, new URI(uri), maxConcurrentRequests);
179                            if (defaultTimeToLive != null)
180                                    channel.setDefaultTimeToLive(defaultTimeToLive);
181                            return channel;
182                    }
183                    catch (URISyntaxException e) {
184                            throw new IllegalArgumentException("Bad uri: " + uri, e);
185                    }
186            }
187    
188            @Override
189            public MessagingChannel newMessagingChannel(String id, String uri) {
190                    try {
191                            MessagingChannel channel = newMessagingChannel(id, new URI(uri));
192                            if (defaultTimeToLive != null)
193                                    channel.setDefaultTimeToLive(defaultTimeToLive);
194                            return channel;
195                    }
196                    catch (URISyntaxException e) {
197                            throw new IllegalArgumentException("Bad uri: " + uri, e);
198                    }
199            }
200    }