001    package railo.runtime.type;
002    
003    import java.io.IOException;
004    import java.io.ObjectInput;
005    import java.io.ObjectOutput;
006    import java.util.HashSet;
007    import java.util.Set;
008    
009    import railo.commons.lang.CFTypes;
010    import railo.commons.lang.ExceptionUtil;
011    import railo.commons.lang.ExternalizableUtil;
012    import railo.commons.lang.SizeOf;
013    import railo.runtime.PageContextImpl;
014    import railo.runtime.PageSource;
015    import railo.runtime.PageSourceImpl;
016    import railo.runtime.config.ConfigWebImpl;
017    import railo.runtime.engine.ThreadLocalPageContext;
018    import railo.runtime.engine.ThreadLocalPageSource;
019    import railo.runtime.exp.ExpressionException;
020    
021    public final class UDFPropertiesImpl implements UDFProperties {
022            public  String functionName;
023            public  int returnType;
024            public  String strReturnType;
025            public  boolean output;
026            public  Boolean bufferOutput;
027            public String hint;
028            public String displayName;
029            //public Page page;
030            public PageSource pageSource;
031            public int index;
032            public FunctionArgument[] arguments;
033            public Struct meta;
034            public String description;
035            public Boolean secureJson;
036            public Boolean verifyClient;
037            public String strReturnFormat;
038            public int returnFormat;
039            public Set<Collection.Key> argumentsSet;
040            public int access;
041            public long cachedWithin; 
042            public Integer localMode;
043    
044            /**
045             * NEVER USE THIS CONSTRUCTOR, this constructor is only for deserialize this object from stream
046             */
047            public UDFPropertiesImpl(){
048                    
049            }
050            
051            
052            public UDFPropertiesImpl(
053                    PageSource pageSource,
054                    FunctionArgument[] arguments,
055                            int index,
056                    String functionName, 
057                    String strReturnType, 
058                    String strReturnFormat, 
059                    boolean output,
060                    int access, 
061                    Boolean bufferOutput,
062                    String displayName, 
063                    String description, 
064                    String hint, 
065                    Boolean secureJson,
066                    Boolean verifyClient,
067                    long cachedWithin,
068                    Integer localMode,
069                    StructImpl meta) throws ExpressionException {
070                    
071                    // this happens when a arcive is based on older source code
072                    if(pageSource==null){
073                            pageSource = ThreadLocalPageSource.get();
074                    }
075                    
076                    
077                    if(arguments.length>0){
078                            this.argumentsSet=new HashSet<Collection.Key>();
079                            for(int i=0;i<arguments.length;i++){
080                                    argumentsSet.add(arguments[i].getName());
081                            }
082                    }
083                    else this.argumentsSet=null;
084                    this.arguments = arguments;
085                    this.description = description;
086                    this.displayName = displayName;
087                    this.functionName = functionName;
088                    this.hint = hint;
089                    this.index = index;
090                    this.meta = meta;
091                    this.output = output;
092                    this.bufferOutput = bufferOutput;
093                    //this.page = PageProxy.toProxy(page);
094                    this.pageSource=pageSource;
095                    
096                    
097                    this.strReturnType=strReturnType;
098                    this.returnType=CFTypes.toShortStrict(strReturnType,CFTypes.TYPE_UNKNOW);
099                    this.strReturnFormat=strReturnFormat;
100                    this.returnFormat=UDFImpl.toReturnFormat(strReturnFormat);
101                    
102                    this.secureJson = secureJson;
103                    this.verifyClient = verifyClient;
104                    this.access = access;
105                    this.cachedWithin=cachedWithin;
106                    this.localMode=localMode;
107            }
108            
109            public UDFPropertiesImpl(
110                    PageSource pageSource,
111                    FunctionArgument[] arguments,
112                            int index,
113                    String functionName, 
114                    short returnType, 
115                    String strReturnFormat, 
116                    boolean output, 
117                    int access, 
118                    Boolean bufferOutput,
119                    String displayName, 
120                    String description, 
121                    String hint, 
122                    Boolean secureJson,
123                    Boolean verifyClient,
124                    long cachedWithin,
125                    Integer localMode,
126                    StructImpl meta) throws ExpressionException {
127                    
128                    // this happens when a arcive is based on older source code
129                    if(pageSource==null){
130                            pageSource = ThreadLocalPageSource.get();
131                    }
132                    
133                    
134                    if(arguments.length>0){
135                            this.argumentsSet=new HashSet<Collection.Key>();
136                            for(int i=0;i<arguments.length;i++){
137                                    argumentsSet.add(arguments[i].getName());
138                            }
139                    }
140                    else this.argumentsSet=null;
141                    
142                    this.arguments = arguments;
143                    this.description = description;
144                    this.displayName = displayName;
145                    this.functionName = functionName;
146                    this.hint = hint;
147                    this.index = index;
148                    this.meta = meta;
149                    this.output = output;
150                    this.bufferOutput = bufferOutput;
151                    this.pageSource = pageSource;
152                    
153                    this.strReturnType=CFTypes.toString(returnType,"any");
154                    this.returnType=returnType;
155                    this.strReturnFormat=strReturnFormat;
156                    this.returnFormat=UDFImpl.toReturnFormat(strReturnFormat);
157                    
158                    this.secureJson = secureJson;
159                    this.verifyClient = verifyClient;
160                    this.access = access;
161                    this.cachedWithin=cachedWithin;
162                    this.localMode=localMode;
163            }
164            
165            /**
166             * @deprecated only supported for old compile templates in .ra archives
167             * */
168            public UDFPropertiesImpl(
169                    PageSource pageSource,
170                    FunctionArgument[] arguments,
171                            int index,
172                    String functionName, 
173                    String strReturnType, 
174                    String strReturnFormat, 
175                    boolean output,
176                    int access, 
177                    String displayName, 
178                    String description, 
179                    String hint, 
180                    Boolean secureJson,
181                    Boolean verifyClient,
182                    long cachedWithin,
183                    StructImpl meta) throws ExpressionException {
184                    this(pageSource, arguments, index, functionName, strReturnType, strReturnFormat, 
185                                    output,  access, null,displayName, description, hint, secureJson, verifyClient, cachedWithin,null, meta);
186            }
187            
188            /**
189             * @deprecated only supported for old compile templates in .ra archives
190             * */
191            public UDFPropertiesImpl(
192                    PageSource pageSource,
193                    FunctionArgument[] arguments,
194                            int index,
195                    String functionName, 
196                    short returnType, 
197                    String strReturnFormat, 
198                    boolean output, 
199                    int access, 
200                    String displayName, 
201                    String description, 
202                    String hint, 
203                    Boolean secureJson,
204                    Boolean verifyClient,
205                    long cachedWithin,
206                    StructImpl meta) throws ExpressionException {
207                    this(pageSource, arguments, index, functionName, returnType, strReturnFormat, 
208                                    output,  access,null, displayName, description, hint, secureJson, verifyClient, cachedWithin, null, meta);
209            }
210                    
211            /**
212             * @deprecated only supported for very old compile templates in .ra archives
213             * */
214            public UDFPropertiesImpl(
215                    PageSource pageSource,
216                    FunctionArgument[] arguments,
217                            int index,
218                    String functionName, 
219                    short returnType, 
220                    String strReturnFormat, 
221                    boolean output, 
222                    int access) throws ExpressionException {
223                    this(pageSource, arguments, index, functionName, returnType,strReturnFormat, output, access, null,
224                                    "","", "", null, null, 0L, null, null);
225            }
226    
227            @Override
228            public long sizeOf() {
229                    return 
230                    SizeOf.size(functionName)+
231                    SizeOf.size(returnType)+
232                    SizeOf.size(strReturnType)+
233                    SizeOf.size(output)+
234                    SizeOf.size(bufferOutput)+
235                    SizeOf.size(hint)+
236                    SizeOf.size(index)+
237                    SizeOf.size(displayName)+
238                    SizeOf.size(arguments)+
239                    SizeOf.size(meta)+
240                    SizeOf.size(description)+
241                    SizeOf.size(secureJson)+
242                    SizeOf.size(verifyClient)+
243                    SizeOf.size(strReturnFormat)+
244                    SizeOf.size(returnFormat)+
245                    SizeOf.size(cachedWithin);
246            }
247            
248    
249             /**
250             * @return the access
251             */
252            public int getAccess() {
253                    return access;
254            }
255    
256            public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
257                    try {
258                            PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
259                            ConfigWebImpl cw = (ConfigWebImpl) ThreadLocalPageContext.getConfig(pc);
260                            String path=ExternalizableUtil.readString(in);
261                            pageSource=PageSourceImpl.best(cw.getPageSources(pc,null, path, false,true,true));
262                            
263                    } 
264                    catch (Throwable e) {
265                            e.printStackTrace();
266                            throw ExceptionUtil.toIOException(e);
267                    }
268                    
269                    arguments=(FunctionArgument[]) in.readObject();
270                    access = in.readInt();
271                    index = in.readInt();
272                    returnFormat = in.readInt();
273                    returnType = in.readInt();
274                    description = ExternalizableUtil.readString(in);
275                    displayName = ExternalizableUtil.readString(in);
276                    functionName = ExternalizableUtil.readString(in);
277                    hint = ExternalizableUtil.readString(in);
278                    meta = (Struct) in.readObject();
279                    output = in.readBoolean();
280                    bufferOutput = ExternalizableUtil.readBoolean(in);
281                    secureJson = ExternalizableUtil.readBoolean(in);
282                    strReturnFormat = ExternalizableUtil.readString(in);
283                    strReturnType = ExternalizableUtil.readString(in);
284                    verifyClient = ExternalizableUtil.readBoolean(in);
285                    cachedWithin = in.readLong();
286                    
287                    if(arguments!=null && arguments.length>0){
288                            this.argumentsSet=new HashSet<Collection.Key>();
289                            for(int i=0;i<arguments.length;i++){
290                                    argumentsSet.add(arguments[i].getName());
291                            }
292                    }
293                    
294            }
295    
296    
297            public void writeExternal(ObjectOutput out) throws IOException {
298    
299                    out.writeObject(pageSource.getFullRealpath());
300                    out.writeObject(arguments);
301                    out.writeInt(access);
302                    out.writeInt(index);
303                    out.writeInt(returnFormat);
304                    out.writeInt(returnType);
305                    ExternalizableUtil.writeString(out,description);
306                    ExternalizableUtil.writeString(out,displayName);
307                    ExternalizableUtil.writeString(out,functionName);
308                    ExternalizableUtil.writeString(out,hint);
309                    out.writeObject(meta);
310                    out.writeBoolean(output);
311                    ExternalizableUtil.writeBoolean(out,bufferOutput);
312                    ExternalizableUtil.writeBoolean(out,secureJson);
313                    ExternalizableUtil.writeString(out,strReturnFormat);
314                    ExternalizableUtil.writeString(out,strReturnType);
315                    ExternalizableUtil.writeBoolean(out,verifyClient);
316                    out.writeLong(cachedWithin);
317                    
318                    
319            }
320    
321    
322    
323    
324    }