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.util.ArrayList;
024    import java.util.Arrays;
025    import java.util.List;
026    
027    import org.granite.client.messaging.channel.amf.JMFAMFMessagingChannel;
028    import org.granite.client.messaging.channel.amf.JMFAMFRemotingChannel;
029    import org.granite.client.messaging.jmf.ClientSharedContext;
030    import org.granite.client.messaging.jmf.DefaultClientSharedContext;
031    import org.granite.client.messaging.jmf.ext.ClientEntityCodec;
032    import org.granite.client.messaging.transport.Transport;
033    import org.granite.client.platform.Platform;
034    import org.granite.messaging.jmf.DefaultCodecRegistry;
035    import org.granite.messaging.jmf.codec.ExtendedObjectCodec;
036    import org.granite.messaging.reflect.Reflection;
037    import org.granite.util.ContentType;
038    import org.granite.util.JMFAMFUtil;
039    
040    /**
041     * @author Franck WOLFF
042     */
043    public class JMFChannelFactory extends AbstractChannelFactory {
044    
045            private ClientSharedContext sharedContext = null;
046            
047            private List<ExtendedObjectCodec> extendedCodecs = null;
048            private List<String> defaultStoredStrings = null;
049            private Reflection reflection = null;
050            
051            public JMFChannelFactory() {
052                    super(ContentType.JMF_AMF);
053            }
054            
055            public JMFChannelFactory(Object context) {
056                    super(ContentType.JMF_AMF, context);
057            }
058    
059            public JMFChannelFactory(Object context, ClientSharedContext sharedContext, Transport remotingTransport, Transport messagingTransport) {
060                    super(ContentType.JMF_AMF, context, remotingTransport, messagingTransport);
061                    
062                    this.sharedContext = sharedContext;
063            }
064    
065            public ClientSharedContext getSharedContext() {
066                    return sharedContext;
067            }
068    
069            public void setSharedContext(ClientSharedContext sharedContext) {
070                    this.sharedContext = sharedContext;
071            }
072            
073            public List<ExtendedObjectCodec> getExtendedCodecs() {
074                    return extendedCodecs;
075            }
076    
077            public void setExtendedCodecs(List<ExtendedObjectCodec> extendedCodecs) {
078                    this.extendedCodecs = extendedCodecs;
079            }
080    
081            public List<String> getDefaultStoredStrings() {
082                    return defaultStoredStrings;
083            }
084    
085            public void setDefaultStoredStrings(List<String> defaultStoredStrings) {
086                    this.defaultStoredStrings = defaultStoredStrings;
087            }
088    
089            public Reflection getReflection() {
090                    return reflection;
091            }
092    
093            public void setReflection(Reflection reflection) {
094                    this.reflection = reflection;
095            }
096    
097            @Override
098            public void start() {
099                    super.start();
100                    
101                    if (sharedContext == null) {
102                            
103                            extendedCodecs = (extendedCodecs != null ? extendedCodecs : new ArrayList<ExtendedObjectCodec>(Arrays.asList(new ClientEntityCodec())));
104                            defaultStoredStrings = (defaultStoredStrings != null ? defaultStoredStrings : new ArrayList<String>(JMFAMFUtil.AMF_DEFAULT_STORED_STRINGS));
105                            reflection = (reflection != null ? reflection : Platform.reflection());
106                            
107                            sharedContext = new DefaultClientSharedContext(new DefaultCodecRegistry(extendedCodecs), defaultStoredStrings, reflection, aliasRegistry);
108                    }
109            }
110    
111            @Override
112            public void stop(boolean stopTransports) {
113                    try {
114                            super.stop(stopTransports);
115                    }
116                    finally {
117                            sharedContext = null;
118                            
119                            extendedCodecs = null;
120                            defaultStoredStrings = null;
121                            reflection = null;
122                    }
123            }
124    
125            @Override
126            public JMFAMFRemotingChannel newRemotingChannel(String id, URI uri) {
127                    return newRemotingChannel(id, uri, RemotingChannel.DEFAULT_MAX_CONCURRENT_REQUESTS);
128            }
129    
130            @Override
131            public JMFAMFRemotingChannel newRemotingChannel(String id, URI uri, int maxConcurrentRequests) {
132                    return new JMFAMFRemotingChannel(getRemotingTransport(), getSharedContext(), id, uri, maxConcurrentRequests);
133            }
134    
135            @Override
136            public JMFAMFMessagingChannel newMessagingChannel(String id, URI uri) {
137                    return new JMFAMFMessagingChannel(getMessagingTransport(), getSharedContext(),id, uri);
138            }
139    }