KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > srcgen > resolve > ClassDescriptorUpdateMethodResolver


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ClassDescriptorUpdateMethodResolver.java,v 1.3 2003/11/27 19:30:08 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP.srcgen.resolve;
9
10 import org.exolab.castor.xml.MarshalException;
11 import org.exolab.castor.xml.ValidationException;
12 import org.ozoneDB.tools.OPP.srcgen.MethodResolver;
13 import org.ozoneDB.tools.OPP.srcgen.ResolverException;
14 import org.ozoneDB.tools.OPP.srcgen.streamfactory.InputStreamFactory;
15 import org.ozoneDB.tools.OPP.castor.Methods;
16 import org.ozoneDB.tools.OPP.castor.OzoneClassDescriptor;
17 import org.ozoneDB.tools.OPP.castor.PublicMethod;
18 import org.ozoneDB.tools.OPP.message.MessageWriter;
19 import org.ozoneDB.tools.OPP.CDHelper;
20 import org.ozoneDB.core.Lock;
21 import org.xml.sax.InputSource JavaDoc;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 /**
29  * Resolves update methods by reading a class descriptor.
30  * @author Joakim Ohlrogge
31  * To change this template use Options | File Templates.
32  */

33 public class ClassDescriptorUpdateMethodResolver implements MethodResolver {
34     private final String JavaDoc WRITELOCK = "WRITE";
35     private final String JavaDoc UPGRADELOCK = "UPGRADE";
36     private final String JavaDoc READLOCK = "READ";
37     //private final String MAXLOCK = "MAX";
38
private final String JavaDoc NONELOCK = "NONE";
39     private InputStreamFactory factory;
40     private MessageWriter msgListener;
41
42
43     public ClassDescriptorUpdateMethodResolver(InputStreamFactory factory, MessageWriter msgListener) {
44         this.factory = factory;
45         this.msgListener = msgListener;
46     }
47
48     public void resolveMethods(String JavaDoc className, MethodResolver.UpdateMethodBag result) throws ResolverException {
49         try {
50             searchUpdateMethods(className, result);
51         } catch (ValidationException e) {
52             throw new ResolverException(e);
53         } catch (MarshalException e) {
54             throw new ResolverException(e);
55         } catch (IOException JavaDoc e) {
56             // This means that the OCD was not found, no error
57
msgListener.info("No ocd was found!");
58             msgListener.debug("message: " + e.getMessage());
59         }
60
61
62     }
63
64     /**
65      * Try to load the XML class descriptor to...
66      */

67     private void searchUpdateMethods(String JavaDoc className, MethodResolver.UpdateMethodBag updateMethods) throws ValidationException, MarshalException, IOException JavaDoc {
68         OzoneClassDescriptor descriptor = null;
69         InputStream JavaDoc input = factory.newInstance(className);
70         descriptor = CDHelper.xml2Descriptor(new InputSource JavaDoc(input));
71
72         Methods mths = descriptor.getMethods();
73         PublicMethod[] m = mths.getPublicMethod();
74         for (int i = 0; i < m.length; i++) {
75             String JavaDoc methodName = m[i].getName();
76             String JavaDoc locklevel = m[i].getLocklevel();
77
78             int level = stringToLockLevel(locklevel);
79
80
81             boolean isUpdate = level > Lock.LEVEL_READ;
82 /* if (isUpdate && updateMethods.keySet().contains(methodName)) {
83                 int oldlevel = ((Integer)updateMethods.get(methodName)).intValue();
84                 if (oldlevel < level){
85                     msgListener.info("'"+methodName+"' has already been marked with a weaker lock level");
86                     msgListener.info("Upgrading '"+methodName+"' from level "+oldlevel+" to level "+ level);
87                     updateMethods.put(methodName, new Integer(level));
88                 }
89                 msgListener.warning("All '" + methodName + "' methods will be marked as update methods.");
90             }*/

91             if (isUpdate) {
92                 updateMethods.addMethod(methodName, level);
93             }
94         }
95     }
96
97     private int stringToLockLevel(String JavaDoc level) {
98         if (WRITELOCK.equals(level)) {
99             return Lock.LEVEL_WRITE;
100         } else if (UPGRADELOCK.equals(level)) {
101             return Lock.LEVEL_UPGRADE;
102         } else if (READLOCK.equals(level)) {
103             return Lock.LEVEL_READ;
104         } else if (NONELOCK.equals(level)) {
105             return Lock.LEVEL_NONE;
106         }
107         return Lock.LEVEL_READ;
108     }
109 }
110
Popular Tags