KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > sfsb > PersistentFileStoreManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.base.sfsb;
25
26 import java.util.ArrayList JavaDoc;
27
28 import java.io.File JavaDoc;
29
30 import java.util.logging.*;
31 import com.sun.logging.*;
32
33 import com.sun.ejb.spi.sfsb.SFSBUUIDUtil;
34 import com.sun.ejb.spi.container.ContainerService;
35
36 import com.sun.ejb.containers.ContainerFactoryImpl;
37
38
39 /**
40  * @author Mahesh Kannan
41  */

42  
43 public class PersistentFileStoreManager
44     extends AbstractFileStoreManager
45 {
46     private SFSBUUIDUtil uuidGenerator;
47
48     /**
49      * No arg constructor
50      */

51     public PersistentFileStoreManager() {
52     }
53
54     protected void onInitialization() {
55
56         try {
57         uuidGenerator = new SimpleKeyGenerator();
58     
59         if (baseDir.isDirectory()) {
60         String JavaDoc[] fileNames = baseDir.list();
61         _logger.log(Level.FINE, storeManagerName
62             + "; removing: " + fileNames.length + " sessions");
63
64         removeExpiredPassivatedSessions(fileNames);
65         }
66     } catch (Exception JavaDoc ex) {
67         _logger.log(Level.WARNING, "ejb.sfsb_persistmgr_oninit_failed",
68             new Object JavaDoc[] {storeManagerName});
69         _logger.log(Level.WARNING, "ejb.sfsb_persistmgr_oninit_failed_exception", ex);
70     }
71     }
72
73     public SFSBUUIDUtil getUUIDUtil() {
74         return uuidGenerator;
75     }
76
77     public void shutdown() {
78         super.shutdown();
79     }
80
81     private void removeExpiredPassivatedSessions(String JavaDoc[] fileNames) {
82     if (fileNames.length > 0) {
83         AsyncFileRemovalTask task = new AsyncFileRemovalTask(
84             this, fileNames);
85         try {
86         ContainerService service = ContainerFactoryImpl.getContainerService();
87
88         //scheduleWork performs the task on the same thread
89
// if it cannot schedule the task for async execution
90
service.scheduleWork(super.getClassLoader(), task);
91         } catch (Throwable JavaDoc th) {
92         //We would be here only if containerService is null
93

94         _logger.log(Level.FINE, storeManagerName
95             + ": Cannot execute file removal aynchronously", th);
96
97         //Execute in the current thread
98
task.run(); //Doesn't throw any exception
99
}
100     }
101     }
102
103     private static class AsyncFileRemovalTask
104     implements Runnable JavaDoc
105     {
106     AbstractFileStoreManager storeManager;
107     String JavaDoc[] fileNames;
108
109     AsyncFileRemovalTask(AbstractFileStoreManager manager,
110         String JavaDoc[] fileNames)
111     {
112         this.storeManager = manager;
113         this.fileNames = fileNames;
114     }
115
116     public void run() {
117         try {
118         int sz = fileNames.length;
119         for (int i=0; i<sz; i++) {
120             storeManager.remove(fileNames[i]);
121         }
122         } catch (Exception JavaDoc ex) {
123         _logger.log(Level.FINE, "Error while removing expired "
124             + "file during startup", ex);
125         }
126     }
127     }
128 }
129
Popular Tags