001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.openwire.tool;
018    
019    import java.io.File;
020    import java.io.PrintWriter;
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.codehaus.jam.JAnnotation;
026    import org.codehaus.jam.JAnnotationValue;
027    import org.codehaus.jam.JClass;
028    import org.codehaus.jam.JProperty;
029    
030    /**
031     * 
032     */
033    public class CSourcesGenerator extends CHeadersGenerator {
034    
035        public Object run() {
036            filePostFix = ".c";
037            if (destFile == null) {
038                destFile = new File(targetDir + "/ow_commands_v" + getOpenwireVersion() + ".c");
039            }
040            return super.run();
041        }
042    
043        protected List sort(List source) {
044            return source;
045        }
046    
047        protected void generateSetup(PrintWriter out) {
048            generateLicence(out);
049            out.println("");
050            out.println("/*****************************************************************************************");
051            out.println(" *  ");
052            out.println(" * NOTE!: This file is auto generated - do not modify!");
053            out.println(" *        if you need to make a change, please see the modify the groovy scripts in the");
054            out.println(" *        under src/gram/script and then use maven openwire:generate to regenerate ");
055            out.println(" *        this file.");
056            out.println(" *  ");
057            out.println(" *****************************************************************************************/");
058            out.println("");
059            out.println("");
060            out.println("#include \"ow_commands_v" + openwireVersion + ".h\"");
061            out.println("");
062            out.println("#define SUCCESS_CHECK( f ) { apr_status_t rc=f; if(rc!=APR_SUCCESS) return rc; }");
063            out.println("");
064        }
065    
066        protected void generateFile(PrintWriter out) throws Exception {
067    
068            ArrayList<JProperty> properties = new ArrayList<JProperty>();
069            jclass.getDeclaredProperties();
070            for (int i = 0; i < jclass.getDeclaredProperties().length; i++) {
071                JProperty p = jclass.getDeclaredProperties()[i];
072                if (isValidProperty(p)) {
073                    properties.add(p);
074                }
075            }
076    
077            String name = jclass.getSimpleName();
078            String type = ("ow_" + name).toUpperCase() + "_TYPE";
079            String baseName = "DataStructure";
080            JClass superclass = jclass.getSuperclass();
081            while (superclass.getSuperclass() != null) {
082                if (sortedClasses.contains(superclass)) {
083                    baseName = superclass.getSimpleName();
084                    break;
085                } else {
086                    superclass = superclass.getSuperclass();
087                }
088            }
089    
090            out.println("ow_boolean ow_is_a_" + name + "(ow_DataStructure *object) {");
091            out.println("   if( object == 0 )");
092            out.println("      return 0;");
093            out.println("      ");
094            out.println("   switch(object->structType) {");
095    
096            for (Iterator iterator = sortedClasses.iterator(); iterator.hasNext();) {
097                JClass sub = (JClass)iterator.next();
098                String subtype = "OW_" + sub.getSimpleName().toUpperCase() + "_TYPE";
099                if (jclass.isAssignableFrom(sub) && !isAbstract(sub)) {
100                    out.println("");
101                    out.println("   case " + subtype + ":");
102                }
103            }
104            out.println("");
105            out.println("      return 1;");
106            out.println("   }");
107            out.println("   return 0;");
108            out.println("}");
109    
110            if (!isAbstract(jclass)) {
111                out.println("");
112                out.println("");
113                out.println("ow_" + name + " *ow_" + name + "_create(apr_pool_t *pool) ");
114                out.println("{");
115                out.println("   ow_" + name + " *value = apr_pcalloc(pool,sizeof(ow_" + name + "));");
116                out.println("   if( value!=0 ) {");
117                out.println("      ((ow_DataStructure*)value)->structType = " + type + ";");
118                out.println("   }");
119                out.println("   return value;");
120                out.println("}");
121            }
122    
123            out.println("");
124            out.println("");
125            out.println("apr_status_t ow_marshal1_" + name + "(ow_bit_buffer *buffer, ow_" + name + " *object)");
126            out.println("{");
127            out.println("   ow_marshal1_" + baseName + "(buffer, (ow_" + baseName + "*)object);");
128    
129            for (Iterator<JProperty> iter = properties.iterator(); iter.hasNext();) {
130                JProperty property = iter.next();
131                String propname = toPropertyCase(property.getSimpleName());
132                boolean cached = isCachedProperty(property);
133                JAnnotation annotation = property.getGetter().getAnnotation("openwire:property");
134                JAnnotationValue size = annotation.getValue("size");
135    
136                type = property.getType().getQualifiedName();
137                if (type.equals("boolean")) {
138                    out.println("   ow_bit_buffer_append(buffer, object->" + propname + ");");
139                } else if (type.equals("byte")) {
140                } else if (type.equals("char")) {
141                } else if (type.equals("short")) {
142                } else if (type.equals("int")) {
143                } else if (type.equals("long")) {
144                    out.println("   ow_marshal1_long(buffer, object->" + propname + ");");
145                } else if (type.equals("byte[]")) {
146                    if (size == null) {
147                        out.println("   ow_bit_buffer_append(buffer,  object->" + propname + "!=0 );");
148                    }
149                } else if (type.equals("org.apache.activeio.packet.ByteSequence")) {
150                    if (size == null) {
151                        out.println("   ow_bit_buffer_append(buffer,  object->" + propname + "!=0 );");
152                    }
153                } else if (type.equals("java.lang.String")) {
154                    out.println("   ow_marshal1_string(buffer, object->" + propname + ");");
155                } else {
156                    if (property.getType().isArrayType()) {
157                        if (size != null) {
158                            out.println("   SUCCESS_CHECK(ow_marshal1_DataStructure_array_const_size(buffer, object->" + propname + ", " + size.asInt() + "));");
159                        } else {
160                            out.println("   SUCCESS_CHECK(ow_marshal1_DataStructure_array(buffer, object->" + propname + "));");
161                        }
162                    } else if (isThrowable(property.getType())) {
163                        out.println("   SUCCESS_CHECK(ow_marshal1_throwable(buffer, object->" + propname + "));");
164                    } else {
165                        if (cached) {
166                            out.println("   SUCCESS_CHECK(ow_marshal1_cached_object(buffer, (ow_DataStructure*)object->" + propname + "));");
167                        } else {
168                            out.println("   SUCCESS_CHECK(ow_marshal1_nested_object(buffer, (ow_DataStructure*)object->" + propname + "));");
169                        }
170                    }
171                }
172                out.println("");
173            }
174    
175            out.println("   ");
176            out.println("   return APR_SUCCESS;");
177            out.println("}");
178            out.println("apr_status_t ow_marshal2_" + name + "(ow_byte_buffer *buffer, ow_bit_buffer *bitbuffer, ow_" + name + " *object)");
179            out.println("{");
180            out.println("   ow_marshal2_" + baseName + "(buffer, bitbuffer, (ow_" + baseName + "*)object);   ");
181    
182            for (Iterator<JProperty> iter = properties.iterator(); iter.hasNext();) {
183                JProperty property = iter.next();
184                JAnnotation annotation = property.getGetter().getAnnotation("openwire:property");
185                JAnnotationValue size = annotation.getValue("size");
186                Object propname = toPropertyCase(property.getSimpleName());
187                boolean cached = isCachedProperty(property);
188    
189                type = property.getType().getQualifiedName();
190                if (type.equals("boolean")) {
191                    out.println("   ow_bit_buffer_read(bitbuffer);");
192                } else if (type.equals("byte")) {
193                    out.println("   SUCCESS_CHECK(ow_byte_buffer_append_" + type + "(buffer, object->" + propname + "));");
194                } else if (type.equals("char")) {
195                    out.println("   SUCCESS_CHECK(ow_byte_buffer_append_" + type + "(buffer, object->" + propname + "));");
196                } else if (type.equals("short")) {
197                    out.println("   SUCCESS_CHECK(ow_byte_buffer_append_" + type + "(buffer, object->" + propname + "));");
198                } else if (type.equals("int")) {
199                    out.println("   SUCCESS_CHECK(ow_byte_buffer_append_" + type + "(buffer, object->" + propname + "));");
200                } else if (type.equals("long")) {
201                    out.println("   SUCCESS_CHECK(ow_marshal2_long(buffer, bitbuffer, object->" + propname + "));");
202                } else if (type.equals("byte[]")) {
203                    if (size != null) {
204                        out.println("   SUCCESS_CHECK(ow_marshal2_byte_array_const_size(buffer, object->" + propname + ", " + size.asInt() + "));");
205                    } else {
206                        out.println("   SUCCESS_CHECK(ow_marshal2_byte_array(buffer, bitbuffer, object->" + propname + "));");
207                    }
208                } else if (type.equals("org.apache.activeio.packet.ByteSequence")) {
209                    if (size != null) {
210                        out.println("   SUCCESS_CHECK(ow_marshal2_byte_array_const_size(buffer, object->" + propname + ", " + size.asInt() + "));");
211                    } else {
212                        out.println("   SUCCESS_CHECK(ow_marshal2_byte_array(buffer, bitbuffer, object->" + propname + "));");
213                    }
214                } else if (type.equals("java.lang.String")) {
215                    out.println("   SUCCESS_CHECK(ow_marshal2_string(buffer, bitbuffer, object->" + propname + "));");
216                } else {
217                    if (property.getType().isArrayType()) {
218                        if (size != null) {
219                            out.println("   SUCCESS_CHECK(ow_marshal2_DataStructure_array_const_size(buffer, bitbuffer, object->" + propname + ", " + size.asInt() + "));");
220                        } else {
221                            out.println("   SUCCESS_CHECK(ow_marshal2_DataStructure_array(buffer, bitbuffer, object->" + propname + "));");
222                        }
223                    } else if (isThrowable(property.getType())) {
224                        out.println("   SUCCESS_CHECK(ow_marshal2_throwable(buffer, bitbuffer, object->" + propname + "));");
225                    } else {
226                        if (cached) {
227                            out.println("   SUCCESS_CHECK(ow_marshal2_cached_object(buffer, bitbuffer, (ow_DataStructure*)object->" + propname + "));");
228                        } else {
229                            out.println("   SUCCESS_CHECK(ow_marshal2_nested_object(buffer, bitbuffer, (ow_DataStructure*)object->" + propname + "));");
230                        }
231                    }
232                }
233                out.println("");
234            }
235    
236            out.println("   ");
237            out.println("   return APR_SUCCESS;");
238            out.println("}");
239            out.println("");
240            out.println("apr_status_t ow_unmarshal_" + name + "(ow_byte_array *buffer, ow_bit_buffer *bitbuffer, ow_" + name + " *object, apr_pool_t *pool)");
241            out.println("{");
242            out.println("   ow_unmarshal_" + baseName + "(buffer, bitbuffer, (ow_" + baseName + "*)object, pool);   ");
243    
244            for (Iterator<JProperty> iter = properties.iterator(); iter.hasNext();) {
245                JProperty property = iter.next();
246                JAnnotation annotation = property.getGetter().getAnnotation("openwire:property");
247                JAnnotationValue size = annotation.getValue("size");
248                String propname = toPropertyCase(property.getSimpleName());
249                boolean cached = isCachedProperty(property);
250    
251                type = property.getType().getQualifiedName();
252    
253                if (type.equals("boolean")) {
254                    out.println("   object->" + propname + " = ow_bit_buffer_read(bitbuffer);");
255                } else if (type.equals("byte")) {
256                    out.println("   SUCCESS_CHECK(ow_byte_array_read_" + type + "(buffer, &object->" + propname + "));");
257                } else if (type.equals("char")) {
258                    out.println("   SUCCESS_CHECK(ow_byte_array_read_" + type + "(buffer, &object->" + propname + "));");
259                } else if (type.equals("short")) {
260                    out.println("   SUCCESS_CHECK(ow_byte_array_read_" + type + "(buffer, &object->" + propname + "));");
261                } else if (type.equals("int")) {
262                    out.println("   SUCCESS_CHECK(ow_byte_array_read_" + type + "(buffer, &object->" + propname + "));");
263                } else if (type.equals("long")) {
264                    out.println("   SUCCESS_CHECK(ow_unmarshal_long(buffer, bitbuffer, &object->" + propname + ", pool));");
265                } else if (type.equals("byte[]")) {
266                    if (size != null) {
267                        out.println("   SUCCESS_CHECK(ow_unmarshal_byte_array_const_size(buffer, &object->" + propname + ", " + size.asInt() + ", pool));");
268                    } else {
269                        out.println("   SUCCESS_CHECK(ow_unmarshal_byte_array(buffer, bitbuffer, &object->" + propname + ", pool));");
270                    }
271                } else if (type.equals("org.apache.activeio.packet.ByteSequence")) {
272                    if (size != null) {
273                        out.println("   SUCCESS_CHECK(ow_unmarshal_byte_array_const_size(buffer, &object->" + propname + ", " + size.asInt() + ", pool));");
274                    } else {
275                        out.println("   SUCCESS_CHECK(ow_unmarshal_byte_array(buffer, bitbuffer, &object->" + propname + ", pool));");
276                    }
277                } else if (type.equals("java.lang.String")) {
278                    out.println("   SUCCESS_CHECK(ow_unmarshal_string(buffer, bitbuffer, &object->" + propname + ", pool));");
279                } else {
280                    if (property.getType().isArrayType()) {
281                        if (size != null) {
282                            out.println("   SUCCESS_CHECK(ow_unmarshal_DataStructure_array_const_size(buffer, bitbuffer, &object->" + propname + ", " + size.asInt() + ", pool));");
283                        } else {
284                            out.println("   SUCCESS_CHECK(ow_unmarshal_DataStructure_array(buffer, bitbuffer, &object->" + propname + ", pool));");
285                        }
286                    } else if (isThrowable(property.getType())) {
287                        out.println("   SUCCESS_CHECK(ow_unmarshal_throwable(buffer, bitbuffer, &object->" + propname + ", pool));");
288                    } else {
289                        if (cached) {
290                            out.println("   SUCCESS_CHECK(ow_unmarshal_cached_object(buffer, bitbuffer, (ow_DataStructure**)&object->" + propname + ", pool));");
291                        } else {
292                            out.println("   SUCCESS_CHECK(ow_unmarshal_nested_object(buffer, bitbuffer, (ow_DataStructure**)&object->" + propname + ", pool));");
293                        }
294                    }
295                }
296                out.println("");
297            }
298    
299            out.println("   ");
300            out.println("   return APR_SUCCESS;");
301            out.println("}");
302        }
303    
304        protected void generateTearDown(PrintWriter out) {
305            out.println("");
306            out.println("ow_DataStructure *ow_create_object(ow_byte type, apr_pool_t *pool)");
307            out.println("{");
308            out.println("   switch( type ) {");
309            for (Iterator iterator = sortedClasses.iterator(); iterator.hasNext();) {
310                JClass jclass = (JClass)iterator.next();
311                String name = jclass.getSimpleName();
312                String type = ("ow_" + name).toUpperCase() + "_TYPE";
313                if (!isAbstract(jclass)) {
314                    out.println("      case " + type + ": return (ow_DataStructure *)ow_" + name + "_create(pool);");
315                }
316            }
317    
318            out.println("");
319            out.println("   }");
320            out.println("   return 0;");
321            out.println("}");
322            out.println("");
323            out.println("apr_status_t ow_marshal1_object(ow_bit_buffer *buffer, ow_DataStructure *object)");
324            out.println("{");
325            out.println("   switch( object->structType ) {");
326    
327            for (Iterator iterator = sortedClasses.iterator(); iterator.hasNext();) {
328                JClass jclass = (JClass)iterator.next();
329                String name = jclass.getSimpleName();
330                String type = ("ow_" + name).toUpperCase() + "_TYPE";
331                if (!isAbstract(jclass)) {
332                    out.println("      case " + type + ": return ow_marshal1_" + name + "(buffer, (ow_" + name + "*)object);");
333                }
334            }
335    
336            out.println("");
337            out.println("   }");
338            out.println("   return APR_EGENERAL;");
339            out.println("}");
340            out.println("");
341            out.println("apr_status_t ow_marshal2_object(ow_byte_buffer *buffer, ow_bit_buffer *bitbuffer, ow_DataStructure *object)");
342            out.println("{");
343            out.println("   switch( object->structType ) {");
344    
345            for (Iterator iterator = sortedClasses.iterator(); iterator.hasNext();) {
346                JClass jclass = (JClass)iterator.next();
347                String name = jclass.getSimpleName();
348                String type = ("ow_" + name).toUpperCase() + "_TYPE";
349                if (!isAbstract(jclass)) {
350                    out.println("      case " + type + ": return ow_marshal2_" + name + "(buffer, bitbuffer, (ow_" + name + "*)object);");
351                }
352            }
353    
354            out.println("");
355            out.println("   }");
356            out.println("   return APR_EGENERAL;");
357            out.println("}");
358            out.println("");
359            out.println("apr_status_t ow_unmarshal_object(ow_byte_array *buffer, ow_bit_buffer *bitbuffer, ow_DataStructure *object, apr_pool_t *pool)");
360            out.println("{");
361            out.println("   switch( object->structType ) {");
362    
363            for (Iterator iterator = sortedClasses.iterator(); iterator.hasNext();) {
364                JClass jclass = (JClass)iterator.next();
365                String name = jclass.getSimpleName();
366                String type = ("ow_" + name).toUpperCase() + "_TYPE";
367                if (!isAbstract(jclass)) {
368                    out.println("      case " + type + ": return ow_unmarshal_" + name + "(buffer, bitbuffer, (ow_" + name + "*)object, pool);");
369                }
370            }
371    
372            out.println("");
373            out.println("   }");
374            out.println("   return APR_EGENERAL;");
375            out.println("}");
376    
377        }
378    }