KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > CayenneUserDir


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.io.File JavaDoc;
23
24 /**
25  * CayenneUserDir represents a directory where all Cayenne-related information
26  * is stored on the user machine. This is normally a <code>$HOME/.cayenne</code>
27  * directory.
28  *
29  * @author Andrus Adamchik
30  */

31 public class CayenneUserDir {
32
33     protected static CayenneUserDir sharedInstance;
34
35     public static final String JavaDoc CAYENNE_DIR = ".cayenne";
36     
37     /**
38      * A property name for the property that allows to define an alternative
39      * location of Cayenne User Directory (instead of default "$HOME/.cayenne").
40      *
41      * @since 1.1
42      */

43     public static final String JavaDoc ALT_USER_DIR_PROPERTY = "cayenne.userdir";
44
45     protected File JavaDoc cayenneUserDir;
46
47     public static CayenneUserDir getInstance() {
48         if(sharedInstance == null) {
49             sharedInstance = new CayenneUserDir();
50         }
51         return sharedInstance;
52     }
53     
54     
55     /**
56      * Constructor for CayenneUserDir.
57      */

58     protected CayenneUserDir() {
59         super();
60
61         File JavaDoc tmpDir = null;
62         String JavaDoc dirName = System.getProperty(ALT_USER_DIR_PROPERTY);
63
64         if (dirName != null) {
65             tmpDir = new File JavaDoc(dirName);
66         }
67         else {
68             File JavaDoc homeDir = new File JavaDoc(System.getProperty("user.home"));
69             tmpDir = new File JavaDoc(homeDir, CAYENNE_DIR);
70         }
71
72         if (tmpDir.exists() && !tmpDir.isDirectory()) {
73             tmpDir = null;
74         }
75         else if (tmpDir.exists() && !tmpDir.canRead()) {
76             tmpDir = null;
77         }
78         else if (!tmpDir.exists()) {
79             tmpDir.mkdirs();
80             if (!tmpDir.exists()) {
81                 tmpDir = null;
82             }
83         }
84
85         cayenneUserDir = tmpDir;
86     }
87
88     /**
89      * Returns a directory object where all user Cayenne-related configuration is stored.
90      * May return null if the directory is not accessible for whatever reason.
91      */

92     public File JavaDoc getDirectory() {
93         return cayenneUserDir;
94     }
95     
96     /**
97      * Return false if the directory is not accessible for
98      * any reason at least for reading.
99      */

100     public boolean canRead() {
101         return cayenneUserDir != null;
102     }
103     
104     /**
105      * Return false if the directory is not accessible for
106      * any reason at least for reading.
107      */

108     public boolean canWrite() {
109         return cayenneUserDir != null && cayenneUserDir.canWrite();
110     }
111     
112     public File JavaDoc resolveFile(String JavaDoc name) {
113         return new File JavaDoc(cayenneUserDir, name);
114     }
115 }
116
Popular Tags