KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > memoryimpl > StorageFactoryImpl


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 package org.netbeans.mdr.persistence.memoryimpl;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import org.netbeans.mdr.persistence.*;
24
25 /**
26  *
27  * @author pbuzek
28  * @version
29  */

30 public class StorageFactoryImpl extends Object JavaDoc implements StorageFactory {
31     public static final String JavaDoc STORAGE_ID = "org.netbeans.mdr.persistence.memoryimpl.id";
32     public static final String JavaDoc STORAGE_NAME = "org.netbeans.mdr.persistence.memoryimpl.fileName";
33     static final String JavaDoc NULL_STORAGE_ID = ".";
34     private static final MOFID NULL_MOFID = new MOFID(0, NULL_STORAGE_ID);
35
36     private StorageImpl nullStorage;
37     
38     private static final HashMap JavaDoc storages = new HashMap JavaDoc();
39
40     /** Creates new StorageFactoryImpl */
41     public StorageFactoryImpl() {
42     }
43
44     /** Creates instance of class that implements Storage interface.
45      * throws StorageException if the name is not valid name of a Storage
46      */

47     public synchronized Storage createStorage(Map JavaDoc properties) throws StorageException {
48         String JavaDoc name = (String JavaDoc) properties.get(STORAGE_ID); // Not mandatory
49
if (name == null || name.equals(NULL_STORAGE_ID)) {
50             if (nullStorage == null) {
51                 nullStorage = new StorageImpl(NULL_STORAGE_ID, null);
52             }
53             return nullStorage;
54         } else {
55             synchronized (storages) {
56                 if (storages.containsKey(name)) {
57                     throw new RuntimeException JavaDoc("Storage '" + name + "' already created.");
58                 } else {
59                     Storage result = new StorageImpl(name, (String JavaDoc) properties.get(STORAGE_NAME));
60                     storages.put(name, result);
61                     return result;
62                 }
63             }
64         }
65     }
66     
67     /** Serializes content of the given storage. Returns false if the storage was not found.
68      */

69     public static boolean serialize(String JavaDoc storageId) throws StorageException {
70         StorageImpl storage;
71         synchronized (storages) {
72             storage = (StorageImpl) storages.get(storageId);
73         }
74         if (storage == null) {
75             return false;
76         }
77         storage.serialize();
78         return true;
79     }
80     
81     public org.netbeans.mdr.persistence.MOFID createNullMOFID() throws StorageException {
82         return NULL_MOFID;
83     }
84 }
85
Popular Tags