root/trunk/RC1/src/com/gnstudio/nabiro/connectivity/amf/ServiceConnector.as

Revision 164, 33.9 KB (checked in by GiorgioNatili, 5 months ago)
Line 
1package com.gnstudio.nabiro.connectivity.amf
2{
3        /**
4         *
5         * GNstudio nabiro
6         * =====================================================================
7         * Copyright(c) 2009
8         * http://www.gnstudio.com
9         *
10         *
11         *
12         * This file is part of the nabiro flash platform framework
13         *
14         *
15         * nabiro is free software; you can redistribute it and/or modify
16         * it under the terms of the GNU Lesser General Public License as published by
17         * the Free Software Foundation; either version 3 of the License, or
18         * at your option) any later version.
19         *
20         * nabiro is distributed in the hope that it will be useful,
21         * but WITHOUT ANY WARRANTY; without even the implied warranty of
22         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23         * GNU General Public License for more details.
24         *
25         * You should have received a copy of the GNU Lesser General Public License
26         * along with Intelligere SCS; if not, write to the Free Software
27         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28         * =====================================================================
29         *
30         *
31         *
32         *   @package  nabiro
33         *
34         *   @version  0.9
35         *   @idea maker                        Giorgio Natili [ g.natili@gnstudio.com ]
36         *   @author                                    Giorgio Natili [ g.natili@gnstudio.com ]
37         *   
38         *       
39         */
40       
41        import com.gnstudio.nabiro.connectivity.amf.events.RemoteObjectWrapperEvent;
42        import com.gnstudio.nabiro.connectivity.amf.remote.IRemoteMethod;
43        import com.gnstudio.nabiro.connectivity.amf.utils.MethodsQueue;
44        import com.gnstudio.nabiro.connectivity.amf.utils.MethodsQueueElement;
45       
46        import flash.errors.IllegalOperationError;
47        import flash.events.Event;
48        import flash.events.EventDispatcher;
49        import flash.events.IEventDispatcher;
50        import flash.ui.Mouse;
51        import flash.utils.getDefinitionByName;
52        import flash.utils.getQualifiedClassName;
53        import flash.utils.getQualifiedSuperclassName;
54       
55        import mx.collections.ArrayCollection;
56        import mx.managers.CursorManager;
57        import mx.messaging.Channel;
58        import mx.messaging.ChannelSet;
59        import mx.messaging.MessageAgent;
60        import mx.messaging.channels.AMFChannel;
61        import mx.messaging.channels.HTTPChannel;
62        import mx.messaging.channels.NetConnectionChannel;
63        import mx.rpc.AbstractOperation;
64        import mx.rpc.AbstractService;
65        import mx.rpc.AsyncResponder;
66        import mx.rpc.AsyncToken;
67        import mx.rpc.events.FaultEvent;
68        import mx.rpc.events.ResultEvent;
69        import mx.rpc.remoting.RemoteObject;
70       
71        /**
72         * This class implements the singleton pattern.
73         */
74        public class ServiceConnector implements IEventDispatcher {
75               
76            //----------------------------------------------------------------------
77            //
78            //  Class constants
79            //
80            //----------------------------------------------------------------------
81               
82                // CursorManager Events
83                public static const BUSY_CURSOR:String = "onBusyCursor";
84                public static const NORMAL_CURSOR:String = "onNormalCursor";
85               
86                // Admitted channel services
87                public static const SERVICE_REMOTEOBJECT:String                         = "mx.messaging.channels.AMFChannel";
88        public static const SERVICE_NETCONNECTION:String                        = "mx.messaging.channels.NetConnectionChannel";
89        public static const SERVICE_HTTPSERVICE:String                          = "mx.messaging.channels.HTTPChannel";
90        public static const SECURE_SERVICE_REMOTEOBJECT:String          = "mx.messaging.channels.SecureAMFChannel";
91       
92        // Default ServiceConnector settings
93        private const MAX_ALLOWED_CHANNELS:int                          = 1;
94       
95        // Service status inforrmations
96        public static const SERVICE_RUNNING:String                      = "running";
97        public static const SERVICE_STARTING:String             = "starting";
98        public static const SERVICE_READY:String                        = "ready";
99        public static const SERVICE_LOGGED_READY:String         = "loggedReady";
100        public static const SERVICE_LOGGED_OUT:String           = "loggedOut";
101        public static const SERVICE_FAULT:String                        = "fault";
102       
103        // Channel information
104        protected var CHANNEL_ID:String                                         = "my-amf";
105        protected var CHANNEL_CHARSET:String                            = "UTF-8";
106
107            //----------------------------------------------------------------------
108            //
109            //  Class variables
110            //
111            //----------------------------------------------------------------------
112
113        // Singleton
114        private static var connector:ServiceConnector;
115       
116        // IEventDispatcher on which the events are sent
117        private static var handler:IEventDispatcher;
118       
119        // EventDsipatcher instance used in order to implement IEventDispatcher
120                private var eventDispatcher:EventDispatcher;
121               
122                // Channels fields
123                private var channelSet:ChannelSet;
124                private var channel:Channel;
125                private var channelSetToken:AsyncToken;
126                private var channelSetConnected:Boolean;
127                private var defaultServiceConnected:Boolean;
128                private var channelMessageAgent:MessageAgent;
129               
130                // Default call object
131                private var defaultServiceRemoteObject:RemoteObject;
132               
133                // Default call configuration
134                [Bindable]
135                public var defaultSource:String;
136               
137                [Bindable]
138                public var defaultDestination:String;
139               
140                // Methods queue
141                private var methodsQueue:MethodsQueue;
142                private var processingQueue:Boolean;
143               
144
145
146                private static var className:String = getQualifiedClassName(super);
147               
148                //----------------------------------------------------------------------
149            //
150            //  Constructor
151            //
152            //----------------------------------------------------------------------
153
154                /**
155                 * Constructor.
156                 */
157                public function ServiceConnector(/* singleton:ServiceConnectorSingleton */) {
158                       
159                        if (connector != null && getQualifiedSuperclassName(this) != className) throw new Error("This a singleton class!");                     
160                       
161                        methodsQueue = MethodsQueue.getQueeManager();
162                        processingQueue = false;
163                       
164                        eventDispatcher = new EventDispatcher();
165                       
166                        channelSetConnected = false;
167                        defaultServiceConnected = false;
168                       
169                        // Composition bug fixing
170                        var amf:AMFChannel = null;
171                        var http:HTTPChannel = null;
172                        var nc:NetConnectionChannel = null;
173                }
174               
175                /**
176                 * Method used to update the singleton instances defined trough sub class
177                 *
178                 */
179                 public function updateInstance(instance:ServiceConnector):ServiceConnector {
180                       
181                        /* var buffer:ByteArray = new ByteArray();
182               
183                buffer.writeObject(ServiceConnector.connector);
184                buffer.position = 0;
185               
186                var result:* = (buffer.readObject()  as ServiceConnector );
187               
188                buffer.position = 0;
189               
190                return result; */
191                       
192                        if (instance != null && instance is ServiceConnector){
193                               
194                                ServiceConnector.connector = instance;
195                               
196                        }else{
197                               
198                                throw new Error("Incorrect Singleton update");
199                               
200                        }
201                       
202                        /* if(target){
203                               
204                                // Define the target that will get the evens
205                        //      ServiceConnector.handler = target;
206                               
207                                // Define the listener for the target
208                                //ServiceConnector.connector.registerTarget();
209                               
210                        }  */
211                       
212                        return ServiceConnector.connector;
213               
214        }
215               
216                /**
217                 * Method used in order to get the ServiceConnector instance and
218                 * in order to define the object against which the events are
219                 * dispatched;
220                 * if you call the method and the ServiceConnector is already created
221                 * you'll change the object against with the events are fired
222                 *
223                 * @param handler IEventDispatcher
224                 * @return ServiceConnector
225                 */
226                public static function getConnector(handler:IEventDispatcher = null):ServiceConnector{
227                       
228                        trace("\t The handler is:", handler, "********************")
229                       
230                        if(!ServiceConnector.connector){
231                               
232                                // Create the new instance
233                                ServiceConnector.connector = new ServiceConnector();
234                               
235                                // Set the connector hanlder
236                                if(handler != null){
237                                       
238                                        ServiceConnector.handler = handler;
239                                                                       
240                                }
241                               
242                                // Notify thet the service is starting against the handler or the class                         
243                                if(ServiceConnector.handler){
244                                       
245                                        ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_STARTING, true, true));
246                                       
247                                }else{
248                                       
249                                        ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_STARTING, true, true));
250                                       
251                                }
252                               
253                        }else{
254                               
255                                // Change the connector hanlder
256                                if(ServiceConnector.handler != handler && handler != null){
257                               
258                                        ServiceConnector.handler = handler;
259                                       
260                                }
261                               
262                                // Notify thet the service is already running against the handler or the class                         
263                                if(ServiceConnector.handler){
264                                       
265                                        ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_RUNNING, true, true));
266                                       
267                                }else{
268                                       
269                                        ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_RUNNING, true, true));
270                                       
271                                }
272                                       
273                               
274                        }
275                       
276                        // Return the reference
277                        return ServiceConnector.connector;
278                }
279               
280                private var _serviceRoot:String;
281               
282                /**
283                 * Getter used to recover the default url
284                 * @return String
285                 */
286                public function get serviceRoot():String{
287                       
288                        return _serviceRoot
289                       
290                }
291               
292                /**
293                 * Initialize the connector providing url and serviceType (AMFChannel,
294                 * HTTPChannel, NetConnectionChannel)
295                 * and the optinal username and password needed for secure services
296                 *
297                 * @param url String
298                 * @param serviceType String
299                 * @param username String
300                 * @param password String
301                 */
302                public function initializeConnector(url:String, serviceType:String, username:String = "", password:String = ""):void{
303                       
304                        _serviceRoot = url;
305                       
306                        // If the channelSet is connected
307                        if(channelSetConnected){
308                               
309                                // Notify thet the service is already running against the handler or the class                         
310                                if(ServiceConnector.handler){
311                                       
312                                        ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_RUNNING, true, true));
313                                       
314                                }else{
315                                       
316                                        ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_RUNNING, true, true));
317                                       
318                                }
319                               
320                                // Check if the defaultRemoteObject is intialized
321                                if(!defaultServiceConnected){
322                                       
323                                        initializeDefaultRemoteObject(url);
324                                       
325                                }
326                               
327                                return;
328                               
329                        }
330                       
331                        // If no channelSet is defined
332                        if(!channelSet){
333                               
334                                // I create a new ont
335                                channelSet = new ChannelSet();
336                               
337                        }
338                       
339                        // Check if too much channels have been added to the channelSet
340                        if(channelSet.channels.length > MAX_ALLOWED_CHANNELS){
341                               
342                                throw(new IllegalOperationError("Attention, this ServiceConnector is studied to handle only " + MAX_ALLOWED_CHANNELS + " channel!"));
343                               
344                        }else{
345                               
346                                // Get trough composition the kind of channel (AMFChannel, HTTPChannel, NetConnectionChannel)
347                                var channelClass:Class = getDefinitionByName(serviceType) as Class;                                     
348                                channel = new channelClass(CHANNEL_ID, url);
349                               
350                                // Add the channel to the channelSet
351                                channelSet.addChannel(channel);
352                               
353                                // If username and password are provided
354                                if(username != "" && password != ""){
355                                       
356                                        // The channelSet is logged in
357                                        channelSetToken = channelSet.login(username, password, CHANNEL_CHARSET);
358                                        channelSetToken.addResponder(new AsyncResponder(onChannelReady, onChannelFault));
359                                       
360                                        try{
361                                               
362                                                CursorManager.setBusyCursor();
363                                       
364                                        }catch(error:Error){
365                                               
366                                                flash.ui.Mouse.show();
367                                               
368                                                if(ServiceConnector.handler){
369                                               
370                                                        ServiceConnector.handler.dispatchEvent(new Event(BUSY_CURSOR, true, true));
371                                                       
372                                                }else{
373                                               
374                                                        this.dispatchEvent(new Event(BUSY_CURSOR, true, true));
375                                               
376                                                }
377                                               
378                                        }       
379                                       
380                                // If no login and password are provided       
381                                }else{                                                 
382                                       
383                                        // Check if the default source and destination are set
384                                        if(!defaultSource ||  defaultSource == ""){
385                                               
386                                                throw(new IllegalOperationError("Attention, the defaultSource is not initialized!"));
387                                                return;
388                                               
389                                        }
390                                       
391                                        if(!defaultDestination ||  defaultDestination == ""){
392                                               
393                                                throw(new IllegalOperationError("Attention, the defaultDestination is not initialized!"));
394                                                return;
395                                               
396                                        }
397                                       
398                                        // Initialize the defaultRemoteObject
399                                        initializeDefaultRemoteObject(url);
400                                       
401                                        // Notify thet the the SERVICE_READY event against the handler or the class                             
402                                        if(ServiceConnector.handler){
403                                               
404                                                ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_READY, true, true));
405                                               
406                                        }else{
407                                                       
408                                                ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_READY, true, true));
409                                                       
410                                        }
411                                                                               
412                                }                                                       
413                               
414                        }               
415                       
416                }
417               
418                /**
419                 * Clean up all the connectors
420                 */
421                public function clean():void{
422                       
423                        // Set the flags used from the connector                       
424                        channelSetConnected = false;
425                        defaultServiceConnected = false;
426                       
427                        // Set to null all the staff used from the service connector
428                        channelSet = null;
429                        channelSetToken = null;
430                       
431                        ServiceConnector.handler = null;
432                        ServiceConnector.connector = null;
433                       
434                }
435               
436                /**
437                 * Method used in order to logout from the services and connect with a new user
438                 */                             
439                public function logoutConnector(clear:Boolean = false):void{
440                       
441                        // Logout from the channel
442                        var channelSetToken:AsyncToken = channelSet.logout();
443                        channelSetToken.addResponder(new AsyncResponder(onChannelLogout, onChannelFault));
444                       
445                        try{
446                                               
447                                CursorManager.setBusyCursor();
448                                       
449                                }catch(error:Error){
450                                               
451                                        flash.ui.Mouse.show();
452                                               
453                                if(ServiceConnector.handler){
454                                               
455                                        ServiceConnector.handler.dispatchEvent(new Event(BUSY_CURSOR, true, true));
456                                                       
457                                }else{
458                                               
459                                        this.dispatchEvent(new Event(BUSY_CURSOR, true, true));
460                                               
461                                }
462                                               
463                        }       
464                       
465                        // Disconect channels and channelSet
466                        /* channel.disconnect(channelSet);
467                        channelSet.disconnectAll(); */
468                       
469                        if(     defaultServiceRemoteObject){
470                               
471                                defaultServiceRemoteObject.logout();
472                                defaultServiceRemoteObject.disconnect();
473                       
474                        }
475                       
476                        // Set the flags used from the connector                       
477                        channelSetConnected = false;
478                        defaultServiceConnected = false;
479                       
480                        // Set to null all the staff used from the service connector
481                        channelSet = null;
482                        channelSetToken = null;
483                       
484                        if(clear){
485                               
486                                ServiceConnector.handler = null;
487                                ServiceConnector.connector = null;
488                               
489                        }                       
490                       
491                }
492               
493                protected function initializeDefaultRemoteObject(uri:String):void{
494                       
495                        // Set the channel uri
496                        channel.uri = uri;
497                                       
498                        // Create the defaultServiceRemoteObject RemoteObject
499                        defaultServiceRemoteObject = new RemoteObject();
500                                       
501                        // Define source and destionation trough the static variables
502                        defaultServiceRemoteObject.source = defaultSource;//"com.rbidr.latte.engine.services.UserService";
503                        defaultServiceRemoteObject.destination = defaultDestination;//"UserService";
504                                       
505                        // Use the same channelSet
506                        defaultServiceRemoteObject.channelSet = channelSet;
507                                       
508                        // The default service is connected
509                        defaultServiceConnected = true;
510                       
511                }
512               
513                public function getChannel(url:String = null):ChannelSet{
514                       
515                        // If no channelSet is defined
516                        if(!channelSet){
517                               
518                                throw(new IllegalOperationError("The default channelSet has not been initialized!"));
519                               
520                        }
521                       
522                        return channelSet;
523                       
524                }
525               
526                /**
527                 * Method used to get a service from each location of the application,
528                 * the method return an AbstractService you can use to perform
529                 * operations against a service trough the AbstractService
530                 * getOperation(param:String) method
531                 * @param serviceName String
532                 * @param source String
533                 * @param sameChannel Boolean
534                 * @param requireLogin Boolean
535                 * @param newChannelSet ChannelSet
536                 * @param loginData Object
537                 * @return AbstractService
538                 */
539                public function getServiceByName(serviceName:String, source:String,
540                                                                                 sameChannel:Boolean = true,
541                                                                                 requireLogin:Boolean = false,
542                                                                                 newChannelSet:ChannelSet = null,
543                                                                                 loginData:Object = null):AbstractService{
544                       
545                       
546                        if(!channel){
547                               
548                                throw(new IllegalOperationError("No channels are defined, call the initializeConnector method before!"));
549                                return;
550                               
551                        }
552                       
553                        // Get an instance of the ServiceLocator
554                        var serviceLocator:ServiceLocator = ServiceLocator.getInstance();
555                       
556                        // If the call has to use a different channel
557                        if(!sameChannel){
558                               
559                                // Check the channel parameter
560                                if(!newChannelSet){
561                                       
562                                        throw(new IllegalOperationError("If you want to change the channelSet you must supply a new one!"));
563                                       
564                                }else{
565                                       
566                                        // and set it
567                                        serviceLocator.channelSet = newChannelSet;
568                                       
569                                }                               
570                               
571                        }else{
572                               
573                                // If no different channel is set the channel of the ServiceLocator
574                                serviceLocator.channelSet = channelSet;
575                               
576                        }
577                       
578                        // Get the abstract service
579                        var service:AbstractService = serviceLocator.getService(serviceName, source);
580                       
581                        // Check if this service method need secure information
582                        if(requireLogin){
583                               
584                                // Check the login data information
585                                if(!loginData){
586                                       
587                                        throw(new IllegalOperationError("If you want to set credentials you have to specify login and pasword!"));
588                                       
589                                }else{
590                                       
591                                        // Set the credentials for the service
592                                        (service as RemoteObject).setCredentials(loginData.login, loginData.password);
593                                       
594                                }
595                               
596                        }
597                       
598                        return service;
599                       
600                       
601                }
602               
603                /**
604                 * Initialize the listeners for the defaultServiceRemoteObject RemoteObject
605                 */
606                protected function intializeDefaultServiceRemoteObject():void{
607                       
608                        if(!defaultServiceRemoteObject.hasEventListener(FaultEvent.FAULT)){
609                               
610                                defaultServiceRemoteObject.addEventListener(FaultEvent.FAULT, onDefaultServiceFault);
611                               
612                        }
613                       
614                        if(!defaultServiceRemoteObject.hasEventListener(ResultEvent.RESULT)){
615                               
616                                defaultServiceRemoteObject.addEventListener(ResultEvent.RESULT, onDefaultServiceResult);
617                               
618                        }
619                       
620                }
621               
622                /**
623                * Method used to perform a remote call against a method that require autentication
624                * @param method IRemoteMethod
625                */
626                public function makeLoggedCall(method:IRemoteMethod):AbstractService{
627                       
628                        // trace("              makeLoggedCall:", method.name);
629                       
630                        // Check if the channel is connected
631                        if(!channelSetConnected){
632                               
633                                throw (new IllegalOperationError("You have to be logged in order to make a call like " + method.name + "!"));
634                                return;
635                               
636                        }       
637                       
638                        // Define the remote object that is aware of the data it get
639                        var tmpRO:ResultAwareRemoteObject = new ResultAwareRemoteObject(method.isList, method.returnObject);
640                       
641                        // The source change if the method needs a differen source
642                        if(method.source != ""){
643                               
644                                tmpRO.source = method.source;
645                       
646                        }else{
647                               
648                                tmpRO.source = connector.defaultSource;
649                       
650                        }
651                       
652                        // The destination changes if the method provides a new destination
653                        if(method.destination != ""){
654                               
655                                tmpRO.destination = method.destination;
656                               
657                        }else{
658                               
659                                tmpRO.destination = connector.defaultDestination;
660                               
661                        }
662                                               
663                        // Define the channel
664                        tmpRO.channelSet = channelSet;
665                       
666                        // Define the listeners for the object
667                        tmpRO.addEventListener(ResultEvent.RESULT, getLoggedResults);
668                        tmpRO.addEventListener(FaultEvent.FAULT, getLoggedFault);
669                       
670                        // Recover the operation
671                        var objOperation:AbstractOperation = tmpRO.getOperation(method.name);
672                       
673                        // Create the method element
674                        var element:MethodsQueueElement = new MethodsQueueElement(method, objOperation);
675                       
676                        // Add a method to the queue
677                        methodsQueue.addStep(element);
678                       
679                        // If there are more than one method to execute skip the processing to the result handler
680                        if(methodsQueue.getActualElements() > 1){
681                               
682                                processingQueue = true;
683                                return objOperation.service;
684                               
685                        }else{
686                               
687                                processingQueue = false;
688                               
689                        }
690                                                       
691                        // Call the remote method handling the arguments
692                        if(method.arguments.length > 0){
693                               
694                                objOperation.send.apply(null, method.arguments);
695                               
696                        }else {
697                               
698                                objOperation.send();
699                               
700                        }
701                       
702                        // if(!CursorManager.getInstance() is BusyCursor){
703               
704                 //     CursorManager.setBusyCursor();
705                 
706                 try{
707                                               
708                                                CursorManager.setBusyCursor();
709                                       
710                                        }catch(error:Error){
711                                               
712                                                flash.ui.Mouse.show();
713                                               
714                                                if(ServiceConnector.handler){
715                                               
716                                                        ServiceConnector.handler.dispatchEvent(new Event(BUSY_CURSOR, true, true));
717                                                       
718                                                }else{
719                                               
720                                                        this.dispatchEvent(new Event(BUSY_CURSOR, true, true));
721                                               
722                                                }
723                                               
724                                        }       
725               
726            //}
727                       
728                        return objOperation.service;                   
729                       
730                }
731               
732                /**
733                * Method used in order to handle the results on the logged call
734                * @param e ResultEvent
735                */
736                private function getLoggedResults(e:ResultEvent):void {
737                       
738                        // Check if it's a list
739                        if((e.target as ResultAwareRemoteObject).isList == true && (e.target as ResultAwareRemoteObject).resultObject != null){
740                               
741                                // Create the arrayCollection needed to handle java list
742                                var tmpCollection:ArrayCollection = new ArrayCollection();
743                               
744                                for each(var item:Object in e.result){
745                                       
746                                        tmpCollection.addItem(item as e.target.resultObject);
747                                       
748                                }
749                               
750                                // Send the data to the instance
751                                dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.RESULT, tmpCollection,
752                                                                                                              (e.target.operation as AbstractOperation).service));
753                                       
754                        }else{
755                               
756                                // Send the data to the instance
757                               
758                                if(!(e.target as ResultAwareRemoteObject)){
759                                       
760                                        dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.RESULT, e.result as e.target.resultObject, (e.currentTarget.operation as AbstractOperation).service));
761                                       
762                                }else{
763                                       
764                                        dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.RESULT, e.result, (e.target.operation as AbstractOperation).service));
765                                       
766                                }
767                               
768                                // trace("(e.currentTarget.operation as AbstractOperation).service", (e.currentTarget.operation as AbstractOperation).service)
769                               
770                        }
771                       
772                       
773                       
774                        try{
775                       
776                                CursorManager.removeBusyCursor();
777                       
778                        }catch(error:Error){
779                               
780                                flash.ui.Mouse.show();
781                               
782                                if(ServiceConnector.handler){
783                               
784                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
785                               
786                                }else{
787                               
788                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
789                                       
790                                }
791                       
792                        }
793                       
794                        // Remove the method from the queue
795                        methodsQueue.removeStep();
796                       
797                        if(processingQueue){
798                               
799                                trace("Methods to be processed:", methodsQueue.getActualElements());
800                               
801                                var method:MethodsQueueElement = methodsQueue.getActualStep();
802                                                               
803                                if(method){
804                                       
805                                        trace("                    ", method.remoteMethod.name, method.remoteMethod.destination)
806                                       
807                                }
808                               
809                                // If no more methods the queue processing is stopped
810                                if(!method){
811                                       
812                                        processingQueue = false;
813                                        return;
814                                       
815                                }
816                               
817                               
818                                var tmpRO:ResultAwareRemoteObject = new ResultAwareRemoteObject(method.remoteMethod.isList, method.remoteMethod.returnObject);
819                                tmpRO.source = method.remoteMethod.source;
820                                tmpRO.destination = method.remoteMethod.destination;
821                               
822                                tmpRO.channelSet = channelSet;
823                               
824                        //      var objOperation:AbstractOperation = tmpRO.getOperation(method.name);
825                                var objOperation:AbstractOperation = method.abstractOperation;
826                               
827                                tmpRO.operations = objOperation
828                               
829                                //tmpRO.addEventListener(ResultEvent.RESULT, getLoggedResults);
830                               
831                                if(method.remoteMethod.arguments.length > 0){
832                                       
833                                        objOperation.send.apply(null, method.remoteMethod.arguments);
834                                       
835                                }else {
836                                       
837                                        objOperation.send();
838                                       
839                                }
840                               
841                               
842                               
843                               
844                        //      if(!CursorManager.getInstance() is BusyCursor){
845               
846                 //     CursorManager.setBusyCursor();
847                 
848                 try{
849                                               
850                                                CursorManager.setBusyCursor();
851                                       
852                                        }catch(error:Error){
853                                               
854                                                flash.ui.Mouse.show();
855                                       
856                                                if(ServiceConnector.handler){
857                                               
858                                                        ServiceConnector.handler.dispatchEvent(new Event(BUSY_CURSOR, true, true));
859                                                       
860                                                }else{
861                                               
862                                                        this.dispatchEvent(new Event(BUSY_CURSOR, true, true));
863                                               
864                                                }
865                                               
866                                        }       
867               
868                 //     }       
869                               
870                        }
871                       
872                       
873                }
874               
875                /**
876                * Method used in order to handle the fault on the logged call
877                * @param e FaultEvent
878                */
879                private function getLoggedFault(e:FaultEvent):void{
880                       
881                        trace("Logged result Fault", e)
882                        dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.FAULT, e.fault.rootCause, (e.currentTarget.operation as AbstractOperation).service));
883               
884                        // CursorManager.removeBusyCursor();
885                        try{
886                       
887                                CursorManager.removeBusyCursor();
888                       
889                        }catch(error:Error){
890                               
891                                flash.ui.Mouse.show();
892                               
893                                if(ServiceConnector.handler){
894                               
895                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
896                               
897                                }else{
898                               
899                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
900                                       
901                                }
902                       
903                        }
904                       
905                        // Remove the method from the queue
906                        methodsQueue.removeStep();
907                       
908                }
909               
910                /**
911                * Method used to make the call against the services that doesn't need
912                * the autentication on the server
913                * @param method:String
914                * @param args rest
915                */
916                public function makeAnonymousCall(method:IRemoteMethod):AbstractService{
917                       
918                        // trace("anonymous call", method.name, method.arguments)
919                       
920                        // If the defaultServiceRemoteObject is not ready
921                        if(!defaultServiceConnected){
922                               
923                                // Throw an exception
924                                throw(new IllegalOperationError("The defaultServiceRemoteObject has not been initialized!"));
925                                return;
926                               
927                        }
928                       
929                        // Define an AbstractOperation with the required method
930                        var operation:AbstractOperation;
931                       
932                        // The source change if the method needs a differen source
933                        if(method.source != "" || method.destination != ""){
934                               
935                                // Define the remote object that is aware of the data it get
936                                var tmpRO:ResultAwareRemoteObject = new ResultAwareRemoteObject(method.isList, method.returnObject);
937                               
938                                // The source change if the method needs a differen source
939                                if(method.source != "" && method.source != null){
940                                       
941                                        tmpRO.source = method.source;
942                               
943                                }else{
944                                       
945                                        tmpRO.source = connector.defaultSource;
946                               
947                                }
948                               
949                                // The destination changes if the method provides a new destination
950                                if(method.destination != "" && method.destination != null){
951                                       
952                                        tmpRO.destination = method.destination;
953                                       
954                                }else{
955                                       
956                                        tmpRO.destination = connector.defaultDestination;
957                                       
958                                }                               
959                               
960                                // Define the channel
961                                tmpRO.channelSet = channelSet;
962                               
963                                // Define the listeners for the object
964                                tmpRO.addEventListener(ResultEvent.RESULT, getLoggedResults);
965                                tmpRO.addEventListener(FaultEvent.FAULT, getLoggedFault);
966                               
967                                // Recover the operation
968                                operation = tmpRO.getOperation(method.name);
969                       
970                                                       
971                        }else{
972                               
973                                // Define an AbstractOperation with the required method
974                                 operation = defaultServiceRemoteObject.getOperation(method.name);
975                               
976                                // Initialize the lsitener used from the defaultServiceRemoteObject
977                                intializeDefaultServiceRemoteObject();
978                               
979                        }               
980                       
981                        // Make the call checking if arguments are to supply
982                        if(method.arguments.length > 0){
983                 
984                       if(method.arguments.length == 1 && method.arguments[0] is Array){
985                               
986                        operation.send.apply(null, method.arguments[0]);
987                               
988                       }else{
989                               
990                        operation.send.apply(null, method.arguments);
991                               
992                       }
993                                               
994                }else{
995                 
996                 operation.send();
997                 
998                }
999                               
1000            try{
1001                                               
1002                                CursorManager.setBusyCursor();
1003                                       
1004                                }catch(error:Error){
1005                                               
1006                                        flash.ui.Mouse.show();
1007                                       
1008                                        if(ServiceConnector.handler){
1009                                               
1010                                                ServiceConnector.handler.dispatchEvent(new Event(BUSY_CURSOR, true, true));
1011                                                       
1012                                        }else{
1013                                               
1014                                                this.dispatchEvent(new Event(BUSY_CURSOR, true, true));
1015                                               
1016                                        }
1017                                               
1018                                }       
1019               
1020          //  }         
1021               
1022                return operation.service;
1023                       
1024                }
1025               
1026                /**
1027                 * RemoteObjectWrapperEvent event is fired agains the instance of the
1028                 * ServiceLocator class
1029                 *
1030                 * @param e ResultEvent
1031                 */
1032                private function onDefaultServiceResult(e:ResultEvent):void {
1033                       
1034               
1035                        dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.RESULT, e.result, (e.currentTarget.operation as AbstractOperation).service));
1036                        // CursorManager.removeBusyCursor();
1037                       
1038                        try{
1039                       
1040                                CursorManager.removeBusyCursor();
1041                       
1042                        }catch(error:Error){
1043                               
1044                                flash.ui.Mouse.show();
1045                               
1046                                if(ServiceConnector.handler){
1047                               
1048                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1049                               
1050                                }else{
1051                               
1052                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1053                                       
1054                                }
1055                       
1056                        }
1057                       
1058                }
1059               
1060                /**
1061                 * RemoteObjectWrapperEvent event is fired agains the instance of the
1062                 * ServiceLocator class
1063                 *
1064                 * @param e FaultEvent
1065                 */
1066                private function onDefaultServiceFault(e:FaultEvent):void {
1067                                               
1068                        dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.FAULT, e.fault.rootCause, (e.currentTarget.operation as AbstractOperation).service));
1069                        // CursorManager.removeBusyCursor();
1070                       
1071                        try{
1072                       
1073                                CursorManager.removeBusyCursor();
1074                       
1075                        }catch(error:Error){
1076                               
1077                                flash.ui.Mouse.show();
1078                               
1079                                if(ServiceConnector.handler){
1080                               
1081                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1082                               
1083                                }else{
1084                               
1085                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1086                                       
1087                                }
1088                       
1089                        }
1090                       
1091                }
1092               
1093                /**
1094                 * Listener to the channel logout
1095                 * @param e ResultEvent
1096                 * @param t Object
1097                 */
1098                private function onChannelLogout(e:ResultEvent, t:Object):void{
1099                       
1100                        // The default channel is connected
1101                        channelSetConnected = false;
1102                       
1103                        // The event is sent
1104                        if(ServiceConnector.handler){
1105                       
1106                                ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_LOGGED_OUT, true, true));     
1107                               
1108                        }else{
1109                               
1110                                ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_LOGGED_OUT, true, true));
1111                               
1112                        }               
1113                       
1114                        // CursorManager.removeBusyCursor();
1115                       
1116                        try{
1117                       
1118                                CursorManager.removeBusyCursor();
1119                       
1120                        }catch(error:Error){
1121                               
1122                                flash.ui.Mouse.show();
1123                       
1124                                if(ServiceConnector.handler){
1125                               
1126                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1127                               
1128                                }else{
1129                               
1130                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1131                                       
1132                                }
1133                       
1134                        }       
1135                       
1136                }
1137               
1138               
1139                /**
1140                 * Listener to the channelSetToken AsyncToken set the
1141                 * channelSetConnected property and fire on the handler the
1142                 * SERVICE_LOGGED_READY event
1143                 *
1144                 * @param e ResultEvent
1145                 * @param t Object
1146                 */
1147                private function onChannelReady(e:ResultEvent, t:Object):void{
1148                       
1149                        // The default channel is connected
1150                        channelSetConnected = true;
1151                       
1152                        // The event is sent
1153                        if(ServiceConnector.handler){
1154                       
1155                                ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_LOGGED_READY, true, true));   
1156                               
1157                        }else{
1158                               
1159                                ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_LOGGED_READY, true, true));
1160                               
1161                        }               
1162                       
1163                        // CursorManager.removeBusyCursor();
1164                       
1165                        try{
1166                       
1167                                CursorManager.removeBusyCursor();
1168                       
1169                        }catch(error:Error){
1170                               
1171                                flash.ui.Mouse.show();
1172                       
1173                                if(ServiceConnector.handler){
1174                               
1175                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1176                               
1177                                }else{
1178                               
1179                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1180                                       
1181                                }
1182                       
1183                        }       
1184                       
1185                }
1186               
1187                /**
1188                 * Listener to the channelSetToken AsyncToken fire on the handler the
1189                 * SERVICE_FAULT event
1190                 *
1191                 * @param e FaultEvent
1192                 * @param t Object
1193                 */
1194                private function onChannelFault(e:FaultEvent, t:Object):void{
1195                       
1196                        channelSet.removeChannel(channel)
1197                       
1198                        trace("Login Fault", e)
1199                        // CursorManager.removeBusyCursor();
1200                       
1201                        try{
1202                       
1203                                CursorManager.removeBusyCursor();
1204                       
1205                        }catch(error:Error){
1206                               
1207                                flash.ui.Mouse.show();
1208                               
1209                                if(ServiceConnector.handler){
1210                               
1211                                        ServiceConnector.handler.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1212                               
1213                                }else{
1214                               
1215                                        this.dispatchEvent(new Event(NORMAL_CURSOR, true, true));
1216                                       
1217                                }
1218                       
1219                        }
1220                       
1221                        if(ServiceConnector.handler){
1222                       
1223                                ServiceConnector.handler.dispatchEvent(new Event(ServiceConnector.SERVICE_FAULT, true, true));
1224                               
1225                        }else{
1226                               
1227                                ServiceConnector.connector.dispatchEvent(new Event(ServiceConnector.SERVICE_FAULT, true, true));
1228                               
1229                        }
1230                       
1231                }
1232               
1233               
1234                /************************************
1235                * IEventDispatcher immplementation
1236                *************************************/
1237                public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void{
1238                       
1239                        eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
1240                       
1241                }
1242               
1243                public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void      {
1244                       
1245                        eventDispatcher.removeEventListener(type, listener, useCapture);
1246                       
1247                }
1248               
1249                public function dispatchEvent(event:Event):Boolean {
1250                       
1251                        return eventDispatcher.dispatchEvent(event);
1252                }
1253               
1254                public function hasEventListener(type:String):Boolean {
1255                       
1256                        return eventDispatcher.hasEventListener(type);
1257                }
1258               
1259                public function willTrigger(type:String):Boolean {
1260                       
1261                        return eventDispatcher.willTrigger(type);
1262                       
1263                }
1264               
1265        }
1266}
1267
1268internal class ServiceConnectorSingleton{
1269       
1270       
1271}
Note: See TracBrowser for help on using the browser.

1.1.2-pro © 2008-2009 agile42 all rights reserved (this page was served in: 0.250000 sec.)