001: /*
002: * Jalisto - JAva LIght STOrage
003: * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * Xcalia
020: * 71, rue Desnouettes
021: * 75014 Paris - France
022: * http://www.xcalia.com
023: */
024: package org.objectweb.jalisto.se.impl.util;
025:
026: import org.objectweb.jalisto.se.impl.LogicalOid;
027: import org.objectweb.jalisto.se.api.Session;
028:
029: import java.util.Properties;
030: import java.util.Iterator;
031: import java.io.*;
032: import java.net.URL;
033:
034: public class JalistoUtils {
035:
036: public static File loadPropertiesFileIn(String propertiesFilePath,
037: Properties prop) throws IOException {
038: boolean isLoaded = false;
039: File propfile = null;
040: if (!propertiesFilePath.equalsIgnoreCase("")) {
041: propfile = new File(propertiesFilePath);
042: if (propfile.exists() && (!propfile.isDirectory())) {
043: FileInputStream fis = new FileInputStream(propfile);
044: prop.load(fis);
045: isLoaded = true;
046: }
047: if (!isLoaded) {
048: ClassLoader classLoader = Thread.currentThread()
049: .getContextClassLoader();
050: InputStream testIS = classLoader
051: .getResourceAsStream(propertiesFilePath);
052: URL url = classLoader.getResource(propertiesFilePath);
053: propfile = new File(url.getFile());
054: prop.load(testIS);
055: }
056: }
057: return propfile;
058: }
059:
060: public static void truncateProperties(Properties prop) {
061: Iterator keys = prop.keySet().iterator();
062: while (keys.hasNext()) {
063: String key = (String) keys.next();
064: String value = prop.getProperty(key);
065: while (value.endsWith(" ") || value.endsWith("\t")) {
066: value = value.substring(0, value.length() - 1);
067: }
068: prop.setProperty(key, value);
069: }
070: }
071:
072: /**
073: * Gets a substring of the specified string avoiding exceptions.
074: * A negative start position can be used to start n characters from
075: * the end of the string.
076: *
077: * @param str the string to get the substring from
078: * @param start the position to start from, negative means
079: * count back from the end of the string by this many characters
080: * @return substring from start position
081: */
082: public static String substring(String str, int start) {
083: if (str == null) {
084: return null;
085: }
086:
087: // handle negatives, which means last n characters
088: if (start < 0) {
089: start = str.length() + start; // remember start is negative
090: }
091:
092: if (start < 0) {
093: start = 0;
094: }
095:
096: if (start > str.length()) {
097: return "";
098: }
099:
100: return str.substring(start);
101: }
102:
103: public static String arrayToString(Object[] array) {
104: StringBuffer sb = new StringBuffer();
105: sb.append("[");
106: for (short i = 0; i < array.length; i++) {
107: sb.append(array[i]);
108: if (i < array.length - 1) {
109: sb.append(",");
110: }
111: }
112: sb.append("]");
113: return sb.toString();
114: }
115:
116: public static String arrayToString(Object[] array, Session session) {
117: StringBuffer sb = new StringBuffer();
118: sb.append("[");
119: for (short i = 0; i < array.length; i++) {
120: Object o = array[i];
121: if (o instanceof LogicalOid) {
122: sb.append(arrayToString(session.readObjectByOid(o),
123: session));
124: } else {
125: sb.append(o);
126: }
127: if (i < array.length - 1) {
128: sb.append(",");
129: }
130: }
131: sb.append("]");
132: return sb.toString();
133: }
134:
135: public static boolean areEquals(byte[] b1, byte[] b2) {
136: if (b1.length != b2.length) {
137: return false;
138: }
139: for (int i = 0; i < b1.length; i++) {
140: if (b1[i] != b2[i]) {
141: return false;
142: }
143: }
144: return true;
145: }
146:
147: public static boolean areEquals(Object[] b1, Object[] b2) {
148: if (b1.length != b2.length) {
149: return false;
150: }
151: for (int i = 0; i < b1.length; i++) {
152: if (!b1[i].equals(b2[i])) {
153: return false;
154: }
155: }
156: return true;
157: }
158: }
|