001    package railo.runtime.type.scope;
002    
003    
004    
005    /**
006     * creates Local and Argument scopes and recyle it
007     */
008    public final class ScopeFactory {
009        
010        int argumentCounter=0;
011        Argument[] arguments=new Argument[]{
012                new ArgumentImpl(),
013                new ArgumentImpl(),
014                new ArgumentImpl(),
015                new ArgumentImpl(),
016                new ArgumentImpl(),
017                new ArgumentImpl(),
018                new ArgumentImpl(),
019                new ArgumentImpl(),
020                new ArgumentImpl(),
021                new ArgumentImpl(),
022                new ArgumentImpl(),
023                new ArgumentImpl(),
024                new ArgumentImpl(),
025                new ArgumentImpl(),
026                new ArgumentImpl(),
027                new ArgumentImpl(),
028                new ArgumentImpl(),
029                new ArgumentImpl(),
030                new ArgumentImpl()
031        };
032        
033        int localCounter=0;
034        LocalImpl[] locals=new LocalImpl[]{
035                new LocalImpl(),
036                new LocalImpl(),
037                new LocalImpl(),
038                new LocalImpl(),
039                new LocalImpl(),
040                new LocalImpl(),
041                new LocalImpl(),
042                new LocalImpl(),
043                new LocalImpl(),
044                new LocalImpl(),
045                new LocalImpl(),
046                new LocalImpl(),
047                new LocalImpl(),
048                new LocalImpl(),
049                new LocalImpl(),
050                new LocalImpl(),
051                new LocalImpl(),
052                new LocalImpl(),
053                new LocalImpl(),
054                new LocalImpl(),
055                new LocalImpl(),
056                new LocalImpl(),
057                new LocalImpl(),
058                new LocalImpl(),
059                new LocalImpl(),
060                new LocalImpl(),
061                new LocalImpl()
062        };
063            private static int count=0;
064        
065        /**
066         * @return returns a Argument scope
067         */
068        public Argument getArgumentInstance() {
069            if(argumentCounter<arguments.length) {
070                    return arguments[argumentCounter++];
071            }
072            return new ArgumentImpl();
073        }
074    
075        /**
076         * @return retruns a Local Instance
077         */
078        public LocalImpl getLocalInstance() {
079            if(localCounter<locals.length) {
080                return locals[localCounter++];
081            }
082            return new LocalImpl();
083        }
084        
085        /** 
086         * @param argument  recycle a Argument scope for reuse
087         */
088        public void recycle(Argument argument) {
089            if(argumentCounter<=0 || argument.isBind()) return;
090            argument.release();
091            arguments[--argumentCounter]=argument;
092        }
093    
094        /**
095         * @param local recycle a Local scope for reuse
096         */
097        public void recycle(LocalImpl local) {
098            if(localCounter<=0  || local.isBind()) return;
099            local.release();
100            locals[--localCounter]=local;
101        }
102        
103        /**
104         * cast a int scope definition to a string definition
105         * @param scope
106         * @return
107         */
108        public static String toStringScope(int scope, String defaultValue) {
109            switch(scope) {
110            case Scope.SCOPE_APPLICATION:   return "application";
111            case Scope.SCOPE_ARGUMENTS:     return "arguments";
112            case Scope.SCOPE_CALLER:        return "caller";
113            case Scope.SCOPE_CGI:           return "cgi";
114            case Scope.SCOPE_CLIENT:        return "client";
115            case Scope.SCOPE_COOKIE:        return "cookie";
116            case Scope.SCOPE_FORM:          return "form";
117            case ScopeSupport.SCOPE_VAR:         
118            case Scope.SCOPE_LOCAL:         return "local";
119            case Scope.SCOPE_REQUEST:       return "request";
120            case Scope.SCOPE_SERVER:        return "server";
121            case Scope.SCOPE_SESSION:       return "session";
122            case Scope.SCOPE_UNDEFINED:     return "undefined";
123            case Scope.SCOPE_URL:           return "url";
124            case Scope.SCOPE_VARIABLES:     return "variables";
125            case ScopeSupport.SCOPE_CLUSTER:     return "cluster";
126            }
127            
128            
129            
130            return defaultValue;
131        }
132        
133        /* *
134         * return a string list of all scope names
135         * @param orderAsInvoked when true the order of the list is the same as they are invoked by the Undefined Scope, when the value is false the list is returned in a alphabetic order 
136         * @return
137         * /
138        public static String[] getAllScopes(boolean orderAsInvoked) {
139            if(!orderAsInvoked){
140                    return new String[]{
141                                    "application","arguments","caller","cgi","client","cluster","cookie","form","local","request","server","session","url","variables"
142                };
143            }
144            return new String[]{
145                            "local","arguments","variables","cgi","url","form","cookie","client","application","caller","cluster","request","server","session"
146            };
147        }*/
148    
149    }