KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > PersistenceManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form;
21
22 import java.util.*;
23 import org.openide.*;
24 import org.openide.util.Lookup;
25
26 /**
27  * An abstract class which defines interface for persistence managers (being
28  * responsible for loading and saving forms) and provides a basic registration
29  * facility.
30  * PersistenceManager implementations should be able to deal with multiple
31  * forms being saved and loaded by one instance of persistence manager (but
32  * not concurrently).
33  *
34  * @author Ian Formanek, Tomas Pavek
35  */

36
37 public abstract class PersistenceManager {
38
39     // -------------------
40
// abstract interface
41

42     /** This method is used to check if the persistence manager can read the
43      * given form (if it understands the form file format).
44      * @return true if this persistence manager can load the form
45      * @exception PersistenceException if any unexpected problem occurred
46      */

47     public abstract boolean canLoadForm(FormDataObject formObject)
48         throws PersistenceException;
49
50     /** This method loads the form from given data object.
51      * @param formObject FormDataObject representing the form files
52      * @param formModel FormModel to be filled with loaded data
53      * @param nonfatalErrors List to be filled with errors occurred during
54      * loading which are not fatal (but should be reported)
55      * @exception PersistenceException if some fatal problem occurred which
56      * prevents loading the form
57      */

58     public abstract void loadForm(FormDataObject formObject,
59                                   FormModel formModel,
60                                   List nonfatalErrors)
61         throws PersistenceException;
62
63     /** This method saves the form to given data object.
64      * @param formObject FormDataObject representing the form files
65      * @param formModel FormModel to be saved
66      * @param nonfatalErrors List to be filled with errors occurred during
67      * saving which are not fatal (but should be reported)
68      * @exception PersistenceException if some fatal problem occurred which
69      * prevents saving the form
70      */

71     public abstract void saveForm(FormDataObject formObject,
72                                   FormModel formModel,
73                                   List nonfatalErrors)
74         throws PersistenceException;
75
76     // ------------
77
// static registry [provisional only]
78

79     private static List managers;
80     private static List managersByName;
81
82     public static void registerManager(PersistenceManager manager) {
83         getManagersList().add(manager);
84     }
85
86     public static void unregisterManager(PersistenceManager manager) {
87         getManagersList().remove(manager);
88     }
89
90     static void registerManager(String JavaDoc managerClassName) {
91         getManagersNamesList().add(managerClassName);
92     }
93
94     public static Iterator getManagers() {
95         ClassLoader JavaDoc classLoader = null;
96         Iterator iter = getManagersNamesList().iterator();
97         while (iter.hasNext()) { // create managers registered by name
98
if (classLoader == null)
99                 classLoader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
100
101             String JavaDoc pmClassName = (String JavaDoc) iter.next();
102             try {
103                 PersistenceManager manager = (PersistenceManager)
104                     classLoader.loadClass(pmClassName).newInstance();
105                 getManagersList().add(manager);
106             }
107             catch (Exception JavaDoc ex1) {
108                 notifyError(ex1, pmClassName);
109             }
110             catch (LinkageError JavaDoc ex2) {
111                 notifyError(ex2, pmClassName);
112             }
113         }
114         getManagersNamesList().clear(); // [is it OK to lose unsuccessful managers?]
115

116         return getManagersList().iterator();
117     }
118
119     private static List getManagersList() {
120         if (managers == null) {
121             managers = new ArrayList();
122             managers.add(new GandalfPersistenceManager());
123         }
124         return managers;
125     }
126
127     private static List getManagersNamesList() {
128         if (managersByName == null)
129             managersByName = new ArrayList();
130         return managersByName;
131     }
132
133     private static void notifyError(Throwable JavaDoc th, String JavaDoc pmClassName) {
134         String JavaDoc msg = FormUtils.getFormattedBundleString(
135             "FMT_ERR_PersistenceManagerInstantiation", // NOI18N
136
new Object JavaDoc[] { pmClassName });
137
138         ErrorManager errorManager = ErrorManager.getDefault();
139         errorManager.annotate(th, msg);
140         errorManager.notify(ErrorManager.EXCEPTION, th);
141     }
142 }
143
Popular Tags