This commit is contained in:
Christian P. MOMON 2013-06-28 14:40:16 +02:00
parent fe1bb291cd
commit 16c24fa114
26 changed files with 5424 additions and 5424 deletions

View file

@ -1,3 +1,3 @@
Manifest-Version: 1.0 Manifest-Version: 1.0
Class-Path: Class-Path:

View file

@ -1,242 +1,242 @@
/** /**
* @author Christian Momon, June 2008. * @author Christian Momon, June 2008.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
/** /**
* This class defines a content file. * This class defines a content file.
* *
*/ */
public class DataFile public class DataFile
{ {
static public int NOID = 0; static public int NOID = 0;
static public int DEFAULT_SIZE = 0; static public int DEFAULT_SIZE = 0;
// //
protected int id; protected int id;
protected int contentId; protected int contentId;
protected String name; protected String name;
protected long size; protected long size;
protected byte[] data; protected byte[] data;
protected String creationDate; protected String creationDate;
protected String creationUser; protected String creationUser;
/** /**
* *
*/ */
public DataFile() public DataFile()
{ {
this.id = NOID; this.id = NOID;
this.contentId = NOID; this.contentId = NOID;
this.name = null; this.name = null;
this.size = DEFAULT_SIZE; this.size = DEFAULT_SIZE;
this.data = null; this.data = null;
this.creationDate = null; this.creationDate = null;
this.creationUser = null; this.creationUser = null;
} }
/** /**
* *
*/ */
public DataFile(final int contentId, final String name, final long size, final byte[] data) public DataFile(final int contentId, final String name, final long size, final byte[] data)
{ {
this.id = NOID; this.id = NOID;
this.contentId = contentId; this.contentId = contentId;
this.name = name; this.name = name;
this.size = size; this.size = size;
this.data = data; this.data = data;
this.creationDate = null; this.creationDate = null;
this.creationUser = null; this.creationUser = null;
} }
/** /**
* *
*/ */
public DataFile(final String name, final long size, final byte[] data) public DataFile(final String name, final long size, final byte[] data)
{ {
this.id = NOID; this.id = NOID;
this.contentId = NOID; this.contentId = NOID;
this.name = name; this.name = name;
this.size = size; this.size = size;
this.data = data; this.data = data;
this.creationDate = null; this.creationDate = null;
this.creationUser = null; this.creationUser = null;
} }
/** /**
* *
*/ */
public int contentId() public int contentId()
{ {
int result; int result;
result = this.contentId; result = this.contentId;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String creationDate() public String creationDate()
{ {
String result; String result;
result = this.creationDate; result = this.creationDate;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String creationUser() public String creationUser()
{ {
String result; String result;
result = this.creationUser; result = this.creationUser;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public byte[] data() public byte[] data()
{ {
byte[] result; byte[] result;
result = this.data; result = this.data;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int id() public int id()
{ {
int result; int result;
result = this.id; result = this.id;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String name() public String name()
{ {
String result; String result;
result = this.name; result = this.name;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void setContentId(final int contentId) public void setContentId(final int contentId)
{ {
this.contentId = contentId; this.contentId = contentId;
} }
/** /**
* *
*/ */
public void setCreationDate(final String creationDate) public void setCreationDate(final String creationDate)
{ {
if (creationDate == null) if (creationDate == null)
{ {
this.creationDate = ""; this.creationDate = "";
} }
else else
{ {
this.creationDate = creationDate; this.creationDate = creationDate;
} }
} }
/** /**
* *
*/ */
public void setCreationUser(final String creationUser) public void setCreationUser(final String creationUser)
{ {
if (creationUser == null) if (creationUser == null)
{ {
this.creationUser = ""; this.creationUser = "";
} }
else else
{ {
this.creationUser = creationUser; this.creationUser = creationUser;
} }
} }
/** /**
* *
*/ */
public void setData(final byte[] data) public void setData(final byte[] data)
{ {
this.data = data; this.data = data;
} }
/** /**
* *
*/ */
public void setId(final int id) public void setId(final int id)
{ {
this.id = id; this.id = id;
} }
/** /**
* *
*/ */
public void setName(final String name) public void setName(final String name)
{ {
if (name == null) if (name == null)
{ {
this.name = ""; this.name = "";
} }
else else
{ {
this.name = name; this.name = name;
} }
} }
/** /**
* *
*/ */
public void setSize(final long size) public void setSize(final long size)
{ {
if (size >= 0) if (size >= 0)
{ {
this.size = size; this.size = size;
} }
else else
{ {
this.size = 0; this.size = 0;
} }
} }
/** /**
* *
*/ */
public long size() public long size()
{ {
long result; long result;
result = this.size; result = this.size;
// //
return (result); return (result);
} }
} }

View file

@ -1,167 +1,167 @@
/** /**
* @author Christian Momon, June 2008. * @author Christian Momon, June 2008.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* This class is a collection of DataFile objects whit some specific methods. * This class is a collection of DataFile objects whit some specific methods.
* *
*/ */
public class DataFiles extends ArrayList<DataFile> public class DataFiles extends ArrayList<DataFile>
{ {
private static final long serialVersionUID = -4584622422555785456L; private static final long serialVersionUID = -4584622422555785456L;
/** /**
* *
* *
*/ */
public DataFiles() public DataFiles()
{ {
super(); super();
} }
/** /**
* *
* @param source * @param source
*/ */
public DataFiles(final DataFiles source) public DataFiles(final DataFiles source)
{ {
super(source); super(source);
} }
/** /**
* *
*/ */
public DataFiles getByContentId(final int id) public DataFiles getByContentId(final int id)
{ {
DataFiles result = new DataFiles(); DataFiles result = new DataFiles();
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
{ {
DataFile contentFile = this.getByIndex(nDataFile); DataFile contentFile = this.getByIndex(nDataFile);
if (contentFile.contentId() == id) if (contentFile.contentId() == id)
{ {
result.add(contentFile); result.add(contentFile);
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public DataFile getById(final int id) public DataFile getById(final int id)
{ {
DataFile result = null; DataFile result = null;
boolean ended = false; boolean ended = false;
int nDataFile = 0; int nDataFile = 0;
while (!ended) while (!ended)
{ {
if (nDataFile >= this.size()) if (nDataFile >= this.size())
{ {
ended = true; ended = true;
result = null; result = null;
} }
else else
{ {
DataFile contentFile = this.getByIndex(nDataFile); DataFile contentFile = this.getByIndex(nDataFile);
if (id == contentFile.id()) if (id == contentFile.id())
{ {
ended = true; ended = true;
result = contentFile; result = contentFile;
} }
else else
{ {
nDataFile += 1; nDataFile += 1;
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public DataFile getByIndex(final int index) public DataFile getByIndex(final int index)
{ {
DataFile result; DataFile result;
result = super.get(index); result = super.get(index);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public DataFile getByName(final String name) public DataFile getByName(final String name)
{ {
DataFile result = null; DataFile result = null;
if ((name == null) || (name.equals(""))) if ((name == null) || (name.equals("")))
{ {
result = null; result = null;
} }
else else
{ {
boolean ended = false; boolean ended = false;
int dataFileIndex = 0; int dataFileIndex = 0;
while (!ended) while (!ended)
{ {
if (dataFileIndex >= this.size()) if (dataFileIndex >= this.size())
{ {
ended = true; ended = true;
result = null; result = null;
} }
else else
{ {
DataFile contentFile = this.getByIndex(dataFileIndex); DataFile contentFile = this.getByIndex(dataFileIndex);
if (name.equals(contentFile.name())) if (name.equals(contentFile.name()))
{ {
ended = true; ended = true;
result = contentFile; result = contentFile;
} }
else else
{ {
dataFileIndex += 1; dataFileIndex += 1;
} }
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
{ {
DataFile contentFile = this.getByIndex(nDataFile); DataFile contentFile = this.getByIndex(nDataFile);
result.append("== " + contentFile.name() + "\n"); result.append("== " + contentFile.name() + "\n");
result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n"); result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n");
} }
// //
return (result.toString()); return (result.toString());
} }
} }

View file

@ -1,357 +1,357 @@
/** /**
* @author Christian Momon, June 2010. * @author Christian Momon, June 2010.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* This class groups function to help in Calendar manipulation. * This class groups function to help in Calendar manipulation.
* *
* SimpleDateFormat is not used cause does not thread safe? * SimpleDateFormat is not used cause does not thread safe?
*/ */
public class DateHelper public class DateHelper
{ {
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(DateHelper.class); // LoggerFactory.getLogger(DateHelper.class);
static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d"; static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d";
static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d"; static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d";
static final String RAW_DATE_FORMAT = "%04d%02d%02d"; static final String RAW_DATE_FORMAT = "%04d%02d%02d";
static final String ISO_DATE_FORMAT = "%04d-%02d-%02d"; static final String ISO_DATE_FORMAT = "%04d-%02d-%02d";
static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d"; static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d";
static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$"; static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$"; static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$"; static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
/** /**
* *
*/ */
static public String americanFormat(final Calendar time) static public String americanFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH)); result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String europeanFormat(final Calendar time) static public String europeanFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR)); result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public boolean isAmericanFormat(final String date) static public boolean isAmericanFormat(final String date)
{ {
boolean result; boolean result;
if (date == null) if (date == null)
{ {
result = false; result = false;
} }
else else
{ {
result = date.matches(AMERICAN_DATE_PATTERN); result = date.matches(AMERICAN_DATE_PATTERN);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public boolean isEuropeanFormat(final String date) static public boolean isEuropeanFormat(final String date)
{ {
boolean result; boolean result;
if (date == null) if (date == null)
{ {
result = false; result = false;
} }
else else
{ {
result = date.matches(EUROPEAN_DATE_PATTERN); result = date.matches(EUROPEAN_DATE_PATTERN);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public boolean isISOFormat(final String date) static public boolean isISOFormat(final String date)
{ {
boolean result; boolean result;
if (date == null) if (date == null)
{ {
result = false; result = false;
} }
else else
{ {
result = date.matches(ISO_DATE_PATTERN); result = date.matches(ISO_DATE_PATTERN);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String ISOFormat(final Calendar time) static public String ISOFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH)); result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public boolean isRawFormat(final String date) static public boolean isRawFormat(final String date)
{ {
boolean result; boolean result;
if (date == null) if (date == null)
{ {
result = false; result = false;
} }
else else
{ {
result = date.matches(RAW_DATE_PATTERN); result = date.matches(RAW_DATE_PATTERN);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public boolean isValidDate(final String date) static public boolean isValidDate(final String date)
{ {
boolean result; boolean result;
if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date))) if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date)))
{ {
result = true; result = true;
} }
else else
{ {
result = false; result = false;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public Calendar parseAmericanDate(final String date) static public Calendar parseAmericanDate(final String date)
{ {
Calendar result; Calendar result;
Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN); Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN);
Matcher matcher = pattern.matcher(date); Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3)) if ((matcher.find()) && (matcher.groupCount() == 3))
{ {
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(1)) - 1, Integer.parseInt(matcher.group(2))); result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(1)) - 1, Integer.parseInt(matcher.group(2)));
} }
else else
{ {
result = null; result = null;
} }
// //
return (result); return (result);
} }
/** /**
* Note: European parsing test made before the American parsing one. * Note: European parsing test made before the American parsing one.
*/ */
static public Calendar parseDate(final String date) static public Calendar parseDate(final String date)
{ {
Calendar result; Calendar result;
if (isEuropeanFormat(date)) if (isEuropeanFormat(date))
{ {
result = parseEuropeanDate(date); result = parseEuropeanDate(date);
} }
else if (isRawFormat(date)) else if (isRawFormat(date))
{ {
result = parseRawDate(date); result = parseRawDate(date);
} }
else if (isISOFormat(date)) else if (isISOFormat(date))
{ {
result = parseISODate(date); result = parseISODate(date);
} }
else if (isAmericanFormat(date)) else if (isAmericanFormat(date))
{ {
result = parseAmericanDate(date); result = parseAmericanDate(date);
} }
else else
{ {
result = null; result = null;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public Calendar parseEuropeanDate(final String date) static public Calendar parseEuropeanDate(final String date)
{ {
Calendar result; Calendar result;
Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN); Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN);
Matcher matcher = pattern.matcher(date); Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3)) if ((matcher.find()) && (matcher.groupCount() == 3))
{ {
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(1))); result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(1)));
} }
else else
{ {
result = null; result = null;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public Calendar parseISODate(final String date) static public Calendar parseISODate(final String date)
{ {
Calendar result; Calendar result;
Pattern pattern = Pattern.compile(ISO_DATE_PATTERN); Pattern pattern = Pattern.compile(ISO_DATE_PATTERN);
Matcher matcher = pattern.matcher(date); Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3)) if ((matcher.find()) && (matcher.groupCount() == 3))
{ {
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3))); result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
} }
else else
{ {
result = null; result = null;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public Calendar parseRawDate(final String date) static public Calendar parseRawDate(final String date)
{ {
Calendar result; Calendar result;
Pattern pattern = Pattern.compile(RAW_DATE_PATTERN); Pattern pattern = Pattern.compile(RAW_DATE_PATTERN);
Matcher matcher = pattern.matcher(date); Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3)) if ((matcher.find()) && (matcher.groupCount() == 3))
{ {
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3))); result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
} }
else else
{ {
result = null; result = null;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String rawFormat(final Calendar time) static public String rawFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1); result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String shortEuropeanFormat(final Calendar time) static public String shortEuropeanFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1); result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1);
} }
// //
return (result); return (result);
} }
} }

View file

@ -1,111 +1,111 @@
/** /**
* @author Christian Momon, June 2010. * @author Christian Momon, June 2010.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.util.Calendar; import java.util.Calendar;
/** /**
* This class groups function to help in Calendar manipulation. * This class groups function to help in Calendar manipulation.
*/ */
public class DateTimeHelper public class DateTimeHelper
{ {
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(DateTimeHelper.class); // LoggerFactory.getLogger(DateTimeHelper.class);
static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d"; static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
static final String RAW_DATE_FORMAT = "%04d%02d%02d %02d:%02d:%02d"; static final String RAW_DATE_FORMAT = "%04d%02d%02d %02d:%02d:%02d";
static final String ISO_DATE_FORMAT = "%04d-%02d-%02d %02d:%02d:%02d"; static final String ISO_DATE_FORMAT = "%04d-%02d-%02d %02d:%02d:%02d";
static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d"; static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$"; static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$"; static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$"; static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
/** /**
* *
*/ */
static public String americanFormat(final Calendar time) static public String americanFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY), result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String europeanFormat(final Calendar time) static public String europeanFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY), result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String ISOFormat(final Calendar time) static public String ISOFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
time.get(Calendar.SECOND)); time.get(Calendar.SECOND));
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String rawFormat(final Calendar time) static public String rawFormat(final Calendar time)
{ {
String result; String result;
if (time == null) if (time == null)
{ {
result = ""; result = "";
} }
else else
{ {
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
time.get(Calendar.SECOND)); time.get(Calendar.SECOND));
} }
// //
return (result); return (result);
} }
} }

View file

@ -1,228 +1,228 @@
/** /**
* @author Christian Momon, September 2009. * @author Christian Momon, September 2009.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
/** /**
* *
*/ */
public class InternetProxyConfiguration public class InternetProxyConfiguration
{ {
// //
protected String host; protected String host;
protected int port; protected int port;
protected String login; protected String login;
protected String password; protected String password;
/** /**
* *
*/ */
public InternetProxyConfiguration() public InternetProxyConfiguration()
{ {
this.host = ""; this.host = "";
this.port = 0; this.port = 0;
this.login = ""; this.login = "";
this.password = ""; this.password = "";
} }
/** /**
* *
*/ */
public InternetProxyConfiguration(final String host, final int port, final String login, final String password) public InternetProxyConfiguration(final String host, final int port, final String login, final String password)
{ {
// //
if (host == null) if (host == null)
{ {
this.host = ""; this.host = "";
} }
else else
{ {
this.host = host; this.host = host;
} }
// //
this.port = port; this.port = port;
// //
if (login == null) if (login == null)
{ {
this.login = ""; this.login = "";
} }
else else
{ {
this.login = login; this.login = login;
} }
// //
if (password == null) if (password == null)
{ {
this.password = ""; this.password = "";
} }
else else
{ {
this.password = password; this.password = password;
} }
} }
/** /**
* *
*/ */
public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception
{ {
// //
if (host == null) if (host == null)
{ {
this.host = ""; this.host = "";
} }
else else
{ {
this.host = host; this.host = host;
} }
// //
if ((port == null) || (port.trim().length() == 0)) if ((port == null) || (port.trim().length() == 0))
{ {
this.port = 0; this.port = 0;
} }
else else
{ {
try try
{ {
this.port = Integer.parseInt(port); this.port = Integer.parseInt(port);
} }
catch (Exception exception) catch (Exception exception)
{ {
String errorMessage = "Incorrect PROXY port value."; String errorMessage = "Incorrect PROXY port value.";
throw new Exception(errorMessage, exception); throw new Exception(errorMessage, exception);
} }
} }
// //
if (login == null) if (login == null)
{ {
this.login = ""; this.login = "";
} }
else else
{ {
this.login = login; this.login = login;
} }
// //
if (password == null) if (password == null)
{ {
this.password = ""; this.password = "";
} }
else else
{ {
this.password = password; this.password = password;
} }
} }
/** /**
* *
*/ */
public String host() public String host()
{ {
String result; String result;
result = this.host; result = this.host;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public boolean isInitialized() public boolean isInitialized()
{ {
boolean result; boolean result;
if ((this.host.length() > 0) && (this.port > 0)) if ((this.host.length() > 0) && (this.port > 0))
{ {
result = true; result = true;
} }
else else
{ {
result = false; result = false;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String login() public String login()
{ {
String result; String result;
result = this.login; result = this.login;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String password() public String password()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int port() public int port()
{ {
int result; int result;
result = this.port; result = this.port;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
String login; String login;
if (this.login.length() == 0) if (this.login.length() == 0)
{ {
login = ""; login = "";
} }
else else
{ {
login = "********"; login = "********";
} }
String password; String password;
if (this.password.length() == 0) if (this.password.length() == 0)
{ {
password = ""; password = "";
} }
else else
{ {
password = "********"; password = "********";
} }
result = "(" + this.host + "," + this.port + "," + login + "," + password + ")"; result = "(" + this.host + "," + this.port + "," + login + "," + password + ")";
// //
return (result); return (result);
} }
} }

View file

@ -1,120 +1,120 @@
/** /**
* @author Christian Momon, September 2009. * @author Christian Momon, September 2009.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
/** /**
* This class defines a simple average manager. For example, it is useful for * This class defines a simple average manager. For example, it is useful for
* millisecond. The maximum value available in input is one day in millisecond. * millisecond. The maximum value available in input is one day in millisecond.
*/ */
public class SimpleAveragemeter public class SimpleAveragemeter
{ {
// //
protected long sum; protected long sum;
protected long cardinal; protected long cardinal;
protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond. protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond.
/** /**
* *
*/ */
public SimpleAveragemeter() public SimpleAveragemeter()
{ {
this.reset(); this.reset();
} }
/** /**
* *
*/ */
synchronized public void add(final long value) synchronized public void add(final long value)
{ {
// Manage the sum limit. // Manage the sum limit.
if ((this.sum > Long.MAX_VALUE / 2) && (cardinal % 2 == 0)) if ((this.sum > Long.MAX_VALUE / 2) && (cardinal % 2 == 0))
{ {
this.sum = this.sum / 2; this.sum = this.sum / 2;
this.cardinal = this.cardinal / 2; this.cardinal = this.cardinal / 2;
} }
// Add the new value. // Add the new value.
if (this.sum > MAX_ADD) if (this.sum > MAX_ADD)
{ {
this.sum += MAX_ADD; this.sum += MAX_ADD;
this.cardinal += 1; this.cardinal += 1;
} }
else else
{ {
this.sum += value; this.sum += value;
this.cardinal += 1; this.cardinal += 1;
} }
} }
/** /**
* *
*/ */
synchronized public long average() synchronized public long average()
{ {
long result; long result;
if (cardinal == 0) if (cardinal == 0)
{ {
result = 0; result = 0;
} }
else else
{ {
result = sum / cardinal; result = sum / cardinal;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public long cardinal() public long cardinal()
{ {
long result; long result;
result = this.cardinal; result = this.cardinal;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
synchronized public void reset() synchronized public void reset()
{ {
this.sum = 0; this.sum = 0;
this.cardinal = 0; this.cardinal = 0;
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = Long.toString(this.average()); result = Long.toString(this.average());
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public long value() public long value()
{ {
long result; long result;
result = this.average(); result = this.average();
// //
return (result); return (result);
} }
} }

View file

@ -1,151 +1,151 @@
/** /**
* @author Christian Momon, in June 2008. * @author Christian Momon, in June 2008.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.util.Date; import java.util.Date;
/** /**
* *
*/ */
public class SimpleChronometer public class SimpleChronometer
{ {
// //
protected long firstTime; protected long firstTime;
/** /**
* *
*/ */
public SimpleChronometer() public SimpleChronometer()
{ {
this.reset(); this.reset();
} }
/** /**
* *
*/ */
public long interval() public long interval()
{ {
long result; long result;
result = new Date().getTime() - this.firstTime; result = new Date().getTime() - this.firstTime;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void reset() public void reset()
{ {
this.firstTime = new Date().getTime(); this.firstTime = new Date().getTime();
} }
/** /**
* TO BE COMPLETED. * TO BE COMPLETED.
*/ */
static public String humanString(final long interval) static public String humanString(final long interval)
{ {
String result; String result;
if (interval < 1000) if (interval < 1000)
{ {
result = interval + "ms"; result = interval + "ms";
} }
else if (interval < 60 * 1000) else if (interval < 60 * 1000)
{ {
result = interval / 1000 + "," + interval % 1000 + "s"; result = interval / 1000 + "," + interval % 1000 + "s";
} }
else if (interval < 60 * 60 * 1000) else if (interval < 60 * 60 * 1000)
{ {
result = interval / 1000 + "," + interval % 1000 + "s"; result = interval / 1000 + "," + interval % 1000 + "s";
} }
else if (interval < 24 * 60 * 60 * 1000) else if (interval < 24 * 60 * 60 * 1000)
{ {
result = interval / 1000 + "," + interval % 1000 + "s"; result = interval / 1000 + "," + interval % 1000 + "s";
} }
else if (interval < 7 * 24 * 60 * 60 * 1000) else if (interval < 7 * 24 * 60 * 60 * 1000)
{ {
result = interval / 1000 + "," + interval % 1000 + "s"; result = interval / 1000 + "," + interval % 1000 + "s";
} }
else else
// if (interval < 7*24*60*60*1000) // if (interval < 7*24*60*60*1000)
{ {
result = interval / 1000 + "," + interval % 1000 + "s"; result = interval / 1000 + "," + interval % 1000 + "s";
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String shortHumanString(final long interval) static public String shortHumanString(final long interval)
{ {
String result; String result;
if (interval < 1000) if (interval < 1000)
{ {
result = interval + " ms"; result = interval + " ms";
} }
else if (interval < 2 * 1000) else if (interval < 2 * 1000)
{ {
result = interval / 1000 + " seconde"; result = interval / 1000 + " seconde";
} }
else if (interval < 60 * 1000) else if (interval < 60 * 1000)
{ {
result = interval / 1000 + " secondes"; result = interval / 1000 + " secondes";
} }
else if (interval < 2 * 60 * 1000L) else if (interval < 2 * 60 * 1000L)
{ {
result = interval / (60 * 1000L) + " minute"; result = interval / (60 * 1000L) + " minute";
} }
else if (interval < 60 * 60 * 1000L) else if (interval < 60 * 60 * 1000L)
{ {
result = interval / (60 * 1000L) + " minutes"; result = interval / (60 * 1000L) + " minutes";
} }
else if (interval < 2 * 60 * 60 * 1000L) else if (interval < 2 * 60 * 60 * 1000L)
{ {
result = interval / (60 * 60 * 1000L) + " heure"; result = interval / (60 * 60 * 1000L) + " heure";
} }
else if (interval < 24 * 60 * 60 * 1000L) else if (interval < 24 * 60 * 60 * 1000L)
{ {
result = interval / (60 * 60 * 1000L) + " heures"; result = interval / (60 * 60 * 1000L) + " heures";
} }
else if (interval < 2 * 24 * 60 * 60 * 1000L) else if (interval < 2 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (24 * 60 * 60 * 1000L) + " jour"; result = interval / (24 * 60 * 60 * 1000L) + " jour";
} }
else if (interval < 7 * 24 * 60 * 60 * 1000L) else if (interval < 7 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (24 * 60 * 60 * 1000L) + " jours"; result = interval / (24 * 60 * 60 * 1000L) + " jours";
} }
else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L) else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine"; result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine";
} }
else if (interval < 30 * 24 * 60 * 60 * 1000L) else if (interval < 30 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines"; result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines";
} }
else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L) else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois"; result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois";
} }
else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L) else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L)
{ {
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année"; result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année";
} }
else else
{ {
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années"; result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années";
} }
// //
return (result); return (result);
} }
} }

View file

@ -1,383 +1,383 @@
/** /**
* @author Christian Momon, June 2008. * @author Christian Momon, June 2008.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* This class is a collection of String objects with specific methods. It makes * This class is a collection of String objects with specific methods. It makes
* possible to build a string without any copy. The goal is to optimize the * possible to build a string without any copy. The goal is to optimize the
* building of strings where they are lot of concatenation action. * building of strings where they are lot of concatenation action.
*/ */
public class StringConcatenator extends ArrayList<String> public class StringConcatenator extends ArrayList<String>
{ {
private static final long serialVersionUID = -1154185934830213732L; private static final long serialVersionUID = -1154185934830213732L;
public String LINE_SEPARATOR = "\n"; public String LINE_SEPARATOR = "\n";
/** /**
* *
*/ */
public StringConcatenator() public StringConcatenator()
{ {
super(); super();
} }
/** /**
* *
*/ */
public StringConcatenator append(final char character) public StringConcatenator append(final char character)
{ {
StringConcatenator result; StringConcatenator result;
this.add(String.valueOf(character)); this.add(String.valueOf(character));
result = this; result = this;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator append(final String string) public StringConcatenator append(final String string)
{ {
StringConcatenator result; StringConcatenator result;
if (string != null) if (string != null)
{ {
this.add(string); this.add(string);
} }
result = this; result = this;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator append(final StringConcatenator string) public StringConcatenator append(final StringConcatenator string)
{ {
StringConcatenator result; StringConcatenator result;
if (string != null) if (string != null)
{ {
for (int nString = 0; nString < string.size(); nString++) for (int nString = 0; nString < string.size(); nString++)
{ {
this.add(string.getByIndex(nString)); this.add(string.getByIndex(nString));
} }
} }
result = this; result = this;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator appendln() public StringConcatenator appendln()
{ {
StringConcatenator result; StringConcatenator result;
this.add(LINE_SEPARATOR); this.add(LINE_SEPARATOR);
result = this; result = this;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator appendln(final char character) public StringConcatenator appendln(final char character)
{ {
StringConcatenator result; StringConcatenator result;
result = this.append(character).appendln(); result = this.append(character).appendln();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator appendln(final String string) public StringConcatenator appendln(final String string)
{ {
StringConcatenator result; StringConcatenator result;
result = this.append(string).appendln(); result = this.append(string).appendln();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public StringConcatenator appendln(final StringConcatenator string) public StringConcatenator appendln(final StringConcatenator string)
{ {
StringConcatenator result; StringConcatenator result;
result = this.append(string).appendln(); result = this.append(string).appendln();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getByIndex(final int id) public String getByIndex(final int id)
{ {
String result; String result;
result = this.get(id); result = this.get(id);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int lenght() public int lenght()
{ {
int result = 0; int result = 0;
for (int nString = 0; nString < this.size(); nString++) for (int nString = 0; nString < this.size(); nString++)
{ {
result += this.getByIndex(nString).length(); result += this.getByIndex(nString).length();
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
StringBuffer preResult = new StringBuffer(this.lenght()); StringBuffer preResult = new StringBuffer(this.lenght());
for (int nString = 0; nString < this.size(); nString++) for (int nString = 0; nString < this.size(); nString++)
{ {
preResult.append(this.getByIndex(nString)); preResult.append(this.getByIndex(nString));
} }
result = new String(preResult); result = new String(preResult);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void writeInto(final java.io.Writer out) throws IOException public void writeInto(final java.io.Writer out) throws IOException
{ {
for (int nString = 0; nString < this.size(); nString++) for (int nString = 0; nString < this.size(); nString++)
{ {
out.write(this.getByIndex(nString)); out.write(this.getByIndex(nString));
} }
} }
/** /**
* *
*/ */
static public String toString(final String[] strings) static public String toString(final String[] strings)
{ {
String result; String result;
if (strings == null) if (strings == null)
{ {
result = null; result = null;
} }
else else
{ {
StringConcatenator string = new StringConcatenator(); StringConcatenator string = new StringConcatenator();
for (int nString = 0; nString < strings.length; nString++) for (int nString = 0; nString < strings.length; nString++)
{ {
string.append(strings[nString]); string.append(strings[nString]);
if (nString < strings.length - 1) if (nString < strings.length - 1)
{ {
string.append(' '); string.append(' ');
} }
} }
result = string.toString(); result = string.toString();
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringNotNull(final String[] strings) static public String toStringNotNull(final String[] strings)
{ {
String result; String result;
result = toString(strings); result = toString(strings);
if (result == null) if (result == null)
{ {
result = ""; result = "";
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringWithBracket(final String[] strings) static public String toStringWithBracket(final String[] strings)
{ {
String result; String result;
if (strings == null) if (strings == null)
{ {
result = null; result = null;
} }
else else
{ {
StringConcatenator merge = new StringConcatenator(); StringConcatenator merge = new StringConcatenator();
merge.append("["); merge.append("[");
merge.append(toStringWithCommas(strings)); merge.append(toStringWithCommas(strings));
merge.append("]"); merge.append("]");
result = merge.toString(); result = merge.toString();
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringWithBracketNotNull(final String[] strings) static public String toStringWithBracketNotNull(final String[] strings)
{ {
String result; String result;
result = toStringWithBrackets(strings); result = toStringWithBrackets(strings);
if (result == null) if (result == null)
{ {
result = ""; result = "";
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringWithBrackets(final String[] strings) static public String toStringWithBrackets(final String[] strings)
{ {
String result; String result;
if (strings == null) if (strings == null)
{ {
result = null; result = null;
} }
else else
{ {
StringConcatenator merge = new StringConcatenator(); StringConcatenator merge = new StringConcatenator();
for (String string : strings) for (String string : strings)
{ {
merge.append("[").append(string).append("]"); merge.append("[").append(string).append("]");
} }
result = merge.toString(); result = merge.toString();
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringWithCommas(final String[] strings) static public String toStringWithCommas(final String[] strings)
{ {
String result; String result;
if (strings == null) if (strings == null)
{ {
result = null; result = null;
} }
else else
{ {
StringConcatenator merge = new StringConcatenator(); StringConcatenator merge = new StringConcatenator();
for (String string : strings) for (String string : strings)
{ {
if (merge.size() != 0) if (merge.size() != 0)
{ {
merge.append(","); merge.append(",");
} }
merge.append(string); merge.append(string);
} }
result = merge.toString(); result = merge.toString();
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String toStringWithFrenchCommas(final String[] strings) static public String toStringWithFrenchCommas(final String[] strings)
{ {
String result; String result;
if (strings == null) if (strings == null)
{ {
result = null; result = null;
} }
else else
{ {
StringConcatenator merge = new StringConcatenator(); StringConcatenator merge = new StringConcatenator();
for (String string : strings) for (String string : strings)
{ {
if (merge.size() != 0) if (merge.size() != 0)
{ {
merge.append(", "); merge.append(", ");
} }
merge.append(string); merge.append(string);
} }
result = merge.toString(); result = merge.toString();
} }
// //
return (result); return (result);
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -1,77 +1,77 @@
/** /**
* @author Christian Momon, March 2010. * @author Christian Momon, March 2010.
* This file is free software under the terms of the GNU Library General Public License * This file is free software under the terms of the GNU Library General Public License
* as published by the Free Software Foundation version 2 or any later version. * as published by the Free Software Foundation version 2 or any later version.
*/ */
package fr.devinsy.util; package fr.devinsy.util;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
/** /**
* *
*/ */
public class StringListWriter extends Writer public class StringListWriter extends Writer
{ {
protected StringList out; protected StringList out;
/** /**
* *
*/ */
StringListWriter() StringListWriter()
{ {
this.out = new StringList(); this.out = new StringList();
} }
/** /**
* *
*/ */
StringListWriter(final int size) StringListWriter(final int size)
{ {
this.out = new StringList(size); this.out = new StringList(size);
} }
/* /*
* *
*/ */
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
} }
/* /*
* *
*/ */
@Override @Override
public void flush() throws IOException public void flush() throws IOException
{ {
} }
/* /*
* *
*/ */
public void write(final char c) throws IOException public void write(final char c) throws IOException
{ {
this.out.append(c); this.out.append(c);
} }
/* /*
* *
*/ */
@Override @Override
public void write(final char[] cbuf, final int off, final int len) throws IOException public void write(final char[] cbuf, final int off, final int len) throws IOException
{ {
this.out.append(cbuf.toString().substring(off, len)); this.out.append(cbuf.toString().substring(off, len));
} }
/* /*
* *
*/ */
@Override @Override
public void write(final String string) throws IOException public void write(final String string) throws IOException
{ {
this.out.append(string); this.out.append(string);
} }
} }

View file

@ -1,67 +1,67 @@
package fr.devinsy.util.cmdexec; package fr.devinsy.util.cmdexec;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
*/ */
public class Wrapper public class Wrapper
{ {
static private final Wrapper instance = new Wrapper(); static private final Wrapper instance = new Wrapper();
static private final Logger logger = LoggerFactory.getLogger(Wrapper.class); static private final Logger logger = LoggerFactory.getLogger(Wrapper.class);
/** /**
* return instance of the controller * return instance of the controller
*/ */
public static Wrapper instance() public static Wrapper instance()
{ {
return (instance); return (instance);
} }
/** /**
* *
*/ */
static public StringBuffer wrap(final String ressource) throws IOException static public StringBuffer wrap(final String ressource) throws IOException
{ {
logger.info("Enter"); logger.info("Enter");
StringBuffer result; StringBuffer result;
result = new StringBuffer(); result = new StringBuffer();
BufferedReader buf = new BufferedReader(new FileReader(ressource)); BufferedReader buf = new BufferedReader(new FileReader(ressource));
String ligne; String ligne;
while ((ligne = buf.readLine()) != null) while ((ligne = buf.readLine()) != null)
{ {
result.append(ligne + "\n"); result.append(ligne + "\n");
} }
// //
logger.info("Exit"); logger.info("Exit");
return (result); return (result);
} }
/** /**
* *
*/ */
static public void wrap(final String ressource, final PrintWriter output) throws IOException static public void wrap(final String ressource, final PrintWriter output) throws IOException
{ {
logger.info("Enter"); logger.info("Enter");
BufferedReader buf = new BufferedReader(new FileReader(ressource)); BufferedReader buf = new BufferedReader(new FileReader(ressource));
String ligne; String ligne;
while ((ligne = buf.readLine()) != null) while ((ligne = buf.readLine()) != null)
{ {
output.print(ligne); output.print(ligne);
} }
logger.info("Exit"); logger.info("Exit");
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,115 +1,115 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.io.File; import java.io.File;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
*/ */
public class CachedFile public class CachedFile
{ {
public enum Status public enum Status
{ {
NOT_LOAD, EXPIRED, UPDATED NOT_LOAD, EXPIRED, UPDATED
} }
static private final Logger logger = LoggerFactory.getLogger(CachedFile.class); static private final Logger logger = LoggerFactory.getLogger(CachedFile.class);
protected String sourceName; protected String sourceName;
protected long sourceTime; protected long sourceTime;
protected Status status; protected Status status;
/** /**
* *
*/ */
public CachedFile(final String fileName) public CachedFile(final String fileName)
{ {
this.sourceName = fileName; this.sourceName = fileName;
this.sourceTime = 0; this.sourceTime = 0;
this.status = Status.NOT_LOAD; this.status = Status.NOT_LOAD;
} }
/** /**
* *
*/ */
protected File getSourceFile() protected File getSourceFile()
{ {
File result; File result;
if (this.sourceName == null) if (this.sourceName == null)
{ {
result = null; result = null;
} }
else else
{ {
File source = new File(this.sourceName); File source = new File(this.sourceName);
if (!source.exists()) if (!source.exists())
{ {
logger.error("source file defined but not found"); logger.error("source file defined but not found");
result = null; result = null;
} }
else else
{ {
result = source; result = source;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
protected Status getStatus() protected Status getStatus()
{ {
Status result; Status result;
File source = getSourceFile(); File source = getSourceFile();
if (source == null) if (source == null)
{ {
this.status = Status.NOT_LOAD; this.status = Status.NOT_LOAD;
} }
else if (this.sourceTime != source.lastModified()) else if (this.sourceTime != source.lastModified())
{ {
this.status = Status.EXPIRED; this.status = Status.EXPIRED;
} }
result = this.status; result = this.status;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void setNotLoad() public void setNotLoad()
{ {
this.status = Status.NOT_LOAD; this.status = Status.NOT_LOAD;
} }
/** /**
* *
*/ */
public void setUpdated() public void setUpdated()
{ {
File source = getSourceFile(); File source = getSourceFile();
if (source == null) if (source == null)
{ {
this.status = Status.NOT_LOAD; this.status = Status.NOT_LOAD;
} }
else else
{ {
this.sourceTime = source.lastModified(); this.sourceTime = source.lastModified();
this.status = Status.UPDATED; this.status = Status.UPDATED;
} }
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,181 +1,181 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.util.Vector; import java.util.Vector;
/** /**
* *
*/ */
public class EtcGroupFile extends CachedFile public class EtcGroupFile extends CachedFile
{ {
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(EtcGroupFile.class); // LoggerFactory.getLogger(EtcGroupFile.class);
static EtcGroupFile instance = null; static EtcGroupFile instance = null;
protected Groups groups; protected Groups groups;
/** /**
* *
*/ */
protected EtcGroupFile() protected EtcGroupFile()
{ {
super("/etc/group"); super("/etc/group");
this.groups = null; this.groups = null;
} }
/* /*
* *
*/ */
public boolean contains(final String name) public boolean contains(final String name)
{ {
boolean result; boolean result;
Groups groups = updatedGroups(); Groups groups = updatedGroups();
if (groups == null) if (groups == null)
{ {
result = false; result = false;
} }
else else
{ {
result = groups.contains(name); result = groups.contains(name);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Group get(final int gid) public Group get(final int gid)
{ {
Group result; Group result;
Groups groups = updatedGroups(); Groups groups = updatedGroups();
if (groups == null) if (groups == null)
{ {
result = null; result = null;
} }
else else
{ {
result = groups.getByGid(gid); result = groups.getByGid(gid);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Group get(final String name) public Group get(final String name)
{ {
Group result; Group result;
Groups groups = updatedGroups(); Groups groups = updatedGroups();
if (groups == null) if (groups == null)
{ {
result = null; result = null;
} }
else else
{ {
result = groups.getByName(name); result = groups.getByName(name);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Vector<String> getLoginGroups(final String login) public Vector<String> getLoginGroups(final String login)
{ {
Vector<String> result; Vector<String> result;
Groups groups = updatedGroups(); Groups groups = updatedGroups();
result = groups.getLoginGroups(login); result = groups.getLoginGroups(login);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getLoginGroupsString(final String login) public String getLoginGroupsString(final String login)
{ {
String result; String result;
groups = updatedGroups(); groups = updatedGroups();
result = groups.getLoginGroupsString(login); result = groups.getLoginGroupsString(login);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = this.groups.toString(); result = this.groups.toString();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
protected Groups updatedGroups() protected Groups updatedGroups()
{ {
Groups result; Groups result;
if (getStatus() != Status.UPDATED) if (getStatus() != Status.UPDATED)
{ {
this.groups = EtcGroupFileReader.load(); this.groups = EtcGroupFileReader.load();
if (this.groups == null) if (this.groups == null)
{ {
setNotLoad(); setNotLoad();
} }
else else
{ {
setUpdated(); setUpdated();
} }
} }
result = this.groups; result = this.groups;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public EtcGroupFile instance() static public EtcGroupFile instance()
{ {
EtcGroupFile result; EtcGroupFile result;
if (EtcGroupFile.instance == null) if (EtcGroupFile.instance == null)
{ {
EtcGroupFile.instance = new EtcGroupFile(); EtcGroupFile.instance = new EtcGroupFile();
} }
result = EtcGroupFile.instance; result = EtcGroupFile.instance;
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,83 +1,83 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
*/ */
public class EtcGroupFileReader public class EtcGroupFileReader
{ {
static private final Logger logger = LoggerFactory.getLogger(EtcGroupFileReader.class); static private final Logger logger = LoggerFactory.getLogger(EtcGroupFileReader.class);
/** /**
* *
*/ */
static public Groups load() static public Groups load()
{ {
Groups result; Groups result;
BufferedReader file; BufferedReader file;
try try
{ {
file = new BufferedReader(new FileReader("/etc/group")); file = new BufferedReader(new FileReader("/etc/group"));
} }
catch (FileNotFoundException exception) catch (FileNotFoundException exception)
{ {
logger.error("File not found"); logger.error("File not found");
file = null; file = null;
} }
if (file == null) if (file == null)
{ {
result = null; result = null;
} }
else else
{ {
result = new Groups(); result = new Groups();
try try
{ {
String line; String line;
while ((line = file.readLine()) != null) while ((line = file.readLine()) != null)
{ {
String[] tokens = line.split(":"); String[] tokens = line.split(":");
Group group = new Group(); Group group = new Group();
group.setName(tokens[0]); group.setName(tokens[0]);
group.setPassword(tokens[1]); group.setPassword(tokens[1]);
group.setGid((new Integer(tokens[2])).intValue()); group.setGid((new Integer(tokens[2])).intValue());
// Manage the case of empty shell. // Manage the case of empty shell.
if (tokens.length == 4) if (tokens.length == 4)
{ {
String[] tokensBis = tokens[3].split(","); String[] tokensBis = tokens[3].split(",");
for (int tokenCounter = 0; tokenCounter < tokensBis.length; tokenCounter++) for (int tokenCounter = 0; tokenCounter < tokensBis.length; tokenCounter++)
{ {
group.addMember(tokensBis[tokenCounter]); group.addMember(tokensBis[tokenCounter]);
} }
} }
result.add(group); result.add(group);
} }
file.close(); file.close();
} }
catch (java.io.IOException exception) catch (java.io.IOException exception)
{ {
logger.error("Exception here."); logger.error("Exception here.");
result = null; result = null;
} }
} }
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,167 +1,167 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
*/ */
public class EtcPasswdFile extends CachedFile public class EtcPasswdFile extends CachedFile
{ {
static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFile.class); static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFile.class);
static EtcPasswdFile instance = null; static EtcPasswdFile instance = null;
protected Users users; protected Users users;
/** /**
* *
*/ */
protected EtcPasswdFile() protected EtcPasswdFile()
{ {
super("/etc/passwd"); super("/etc/passwd");
this.users = null; this.users = null;
} }
/* /*
* *
*/ */
public boolean contains(final String login) public boolean contains(final String login)
{ {
boolean result; boolean result;
Users users = updatedUsers(); Users users = updatedUsers();
if (users == null) if (users == null)
{ {
result = false; result = false;
} }
else else
{ {
result = users.contains(login); result = users.contains(login);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public User get(final int uid) public User get(final int uid)
{ {
User result; User result;
Users users = updatedUsers(); Users users = updatedUsers();
if (users == null) if (users == null)
{ {
result = null; result = null;
} }
else else
{ {
result = users.getByUid(uid); result = users.getByUid(uid);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public User get(final String login) public User get(final String login)
{ {
User result; User result;
Users users = updatedUsers(); Users users = updatedUsers();
if (users == null) if (users == null)
{ {
result = null; result = null;
} }
else else
{ {
result = users.getByLogin(login); result = users.getByLogin(login);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = this.users.toString(); result = this.users.toString();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
protected Users update() protected Users update()
{ {
Users result; Users result;
logger.debug("updating"); logger.debug("updating");
this.users = EtcPasswdFileReader.load(); this.users = EtcPasswdFileReader.load();
if (this.users == null) if (this.users == null)
{ {
setNotLoad(); setNotLoad();
} }
else else
{ {
setUpdated(); setUpdated();
} }
result = this.users; result = this.users;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
protected Users updatedUsers() protected Users updatedUsers()
{ {
Users result; Users result;
if (getStatus() != Status.UPDATED) if (getStatus() != Status.UPDATED)
{ {
update(); update();
} }
result = this.users; result = this.users;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public EtcPasswdFile instance() static public EtcPasswdFile instance()
{ {
EtcPasswdFile result; EtcPasswdFile result;
if (EtcPasswdFile.instance == null) if (EtcPasswdFile.instance == null)
{ {
EtcPasswdFile.instance = new EtcPasswdFile(); EtcPasswdFile.instance = new EtcPasswdFile();
} }
result = EtcPasswdFile.instance; result = EtcPasswdFile.instance;
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,86 +1,86 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
*/ */
public class EtcPasswdFileReader public class EtcPasswdFileReader
{ {
static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFileReader.class); static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFileReader.class);
/** /**
* *
*/ */
static public Users load() static public Users load()
{ {
Users result; Users result;
BufferedReader file; BufferedReader file;
try try
{ {
file = new BufferedReader(new FileReader("/etc/passwd")); file = new BufferedReader(new FileReader("/etc/passwd"));
} }
catch (FileNotFoundException exception) catch (FileNotFoundException exception)
{ {
logger.error("File not found"); logger.error("File not found");
file = null; file = null;
} }
if (file == null) if (file == null)
{ {
result = null; result = null;
} }
else else
{ {
result = new Users(); result = new Users();
try try
{ {
String line; String line;
while ((line = file.readLine()) != null) while ((line = file.readLine()) != null)
{ {
String[] tokens = line.split(":"); String[] tokens = line.split(":");
User user = new User(); User user = new User();
user.setLogin(tokens[0]); user.setLogin(tokens[0]);
user.setPassword(tokens[1]); user.setPassword(tokens[1]);
user.setUid((new Integer(tokens[2])).intValue()); user.setUid((new Integer(tokens[2])).intValue());
user.setGid((new Integer(tokens[3])).intValue()); user.setGid((new Integer(tokens[3])).intValue());
user.setRealName(tokens[4]); user.setRealName(tokens[4]);
user.setHomeDirectory(tokens[5]); user.setHomeDirectory(tokens[5]);
// Manage the case of empty shell. // Manage the case of empty shell.
if (tokens.length == 7) if (tokens.length == 7)
{ {
user.setShell(tokens[6]); user.setShell(tokens[6]);
} }
else else
{ {
user.setShell(""); user.setShell("");
} }
result.add(user); result.add(user);
} }
file.close(); file.close();
} }
catch (java.io.IOException exception) catch (java.io.IOException exception)
{ {
logger.error("Exception here."); logger.error("Exception here.");
result = null; result = null;
} }
} }
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,204 +1,204 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.util.Vector; import java.util.Vector;
/** /**
* *
*/ */
public class Group public class Group
{ {
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(Group.class); // LoggerFactory.getLogger(Group.class);
/* /*
* /usr/include/grp.h * /usr/include/grp.h
* *
* The group structure. * The group structure.
* struct group * struct group
* { * {
* char *gr_name; Group name. * char *gr_name; Group name.
* char *gr_passwd; Password. * char *gr_passwd; Password.
* __gid_t gr_gid; Group ID. * __gid_t gr_gid; Group ID.
* char **gr_mem; Member list. * char **gr_mem; Member list.
* }; * };
*/ */
protected String name; protected String name;
protected String password; protected String password;
protected int gid; protected int gid;
protected Vector<String> members; protected Vector<String> members;
/** /**
* *
*/ */
public Group() public Group()
{ {
this.name = null; this.name = null;
this.password = null; this.password = null;
this.gid = -1; this.gid = -1;
this.members = new Vector<String>(); this.members = new Vector<String>();
} }
/** /**
* *
*/ */
public void addMember(final String login) public void addMember(final String login)
{ {
if ((login != null) && (login.length() != 0)) if ((login != null) && (login.length() != 0))
{ {
this.members.add(login); this.members.add(login);
} }
} }
/** /**
* *
*/ */
public int getGid() public int getGid()
{ {
int result; int result;
result = this.gid; result = this.gid;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Vector<String> getMembers() public Vector<String> getMembers()
{ {
Vector<String> result; Vector<String> result;
result = this.members; result = this.members;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getName() public String getName()
{ {
String result; String result;
result = this.name; result = this.name;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getPassword() public String getPassword()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int gid() public int gid()
{ {
int result; int result;
result = this.gid; result = this.gid;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Vector<String> members() public Vector<String> members()
{ {
Vector<String> result; Vector<String> result;
result = this.members; result = this.members;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String name() public String name()
{ {
String result; String result;
result = this.name; result = this.name;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String passwd() public String passwd()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void setGid(final int gid) public void setGid(final int gid)
{ {
this.gid = gid; this.gid = gid;
} }
/** /**
* *
*/ */
public void setName(final String name) public void setName(final String name)
{ {
this.name = name; this.name = name;
} }
/** /**
* *
*/ */
public void setPasswd(final String password) public void setPasswd(final String password)
{ {
this.password = password; this.password = password;
} }
/** /**
* *
*/ */
public void setPassword(final String password) public void setPassword(final String password)
{ {
this.password = password; this.password = password;
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = "|" + this.name + "|" + this.password + "|" + this.gid + "|" + this.members + "|"; result = "|" + this.name + "|" + this.password + "|" + this.gid + "|" + this.members + "|";
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,214 +1,214 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.util.Iterator; import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
/** /**
* *
*/ */
public class Groups extends Vector<Group> public class Groups extends Vector<Group>
{ {
private static final long serialVersionUID = 5802487312198869603L; private static final long serialVersionUID = 5802487312198869603L;
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(Groups.class); // LoggerFactory.getLogger(Groups.class);
/** /**
* *
*/ */
public Groups() public Groups()
{ {
super(); super();
} }
/* /*
* *
*/ */
public boolean contains(final int gid) public boolean contains(final int gid)
{ {
boolean result; boolean result;
if (getByGid(gid) == null) if (getByGid(gid) == null)
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/* /*
* *
*/ */
public boolean contains(final String name) public boolean contains(final String name)
{ {
boolean result; boolean result;
if (getByName(name) == null) if (getByName(name) == null)
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Group getByGid(final int gid) public Group getByGid(final int gid)
{ {
Group result; Group result;
result = null; result = null;
boolean ended = false; boolean ended = false;
Iterator<Group> iterator = this.iterator(); Iterator<Group> iterator = this.iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
{ {
Group group = iterator.next(); Group group = iterator.next();
if (group.getGid() == gid) if (group.getGid() == gid)
{ {
ended = true; ended = true;
result = group; result = group;
} }
} }
else else
{ {
ended = true; ended = true;
result = null; result = null;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Group getByName(final String name) public Group getByName(final String name)
{ {
Group result; Group result;
if (name == null) if (name == null)
{ {
result = null; result = null;
} }
else else
{ {
result = null; result = null;
boolean ended = false; boolean ended = false;
Iterator<Group> iterator = this.iterator(); Iterator<Group> iterator = this.iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
{ {
Group group = iterator.next(); Group group = iterator.next();
if (group.getName().equals(name)) if (group.getName().equals(name))
{ {
ended = true; ended = true;
result = group; result = group;
} }
} }
else else
{ {
ended = true; ended = true;
result = null; result = null;
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Vector<String> getLoginGroups(final String login) public Vector<String> getLoginGroups(final String login)
{ {
Vector<String> result; Vector<String> result;
result = new Vector<String>(); result = new Vector<String>();
Iterator<Group> iterator = this.iterator(); Iterator<Group> iterator = this.iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
Group group = iterator.next(); Group group = iterator.next();
if (group.members().contains(login)) if (group.members().contains(login))
{ {
result.add(group.getName()); result.add(group.getName());
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getLoginGroupsString(final String login) public String getLoginGroupsString(final String login)
{ {
String result; String result;
Vector<String> groups = getLoginGroups(login); Vector<String> groups = getLoginGroups(login);
StringBuffer string = new StringBuffer(); StringBuffer string = new StringBuffer();
for (String group : groups) for (String group : groups)
{ {
if (string.length() == 0) if (string.length() == 0)
{ {
string.append(group); string.append(group);
} }
else else
{ {
string.append(","); string.append(",");
string.append(group); string.append(group);
} }
} }
result = string.toString(); result = string.toString();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
StringBuffer out = new StringBuffer(); StringBuffer out = new StringBuffer();
Iterator<Group> iterator = this.iterator(); Iterator<Group> iterator = this.iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
out.append(iterator.next().toString() + "\n"); out.append(iterator.next().toString() + "\n");
} }
result = out.toString(); result = out.toString();
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

File diff suppressed because it is too large Load diff

View file

@ -1,337 +1,337 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
/** /**
* *
*/ */
public class User public class User
{ {
// static private final Logger logger = LoggerFactory.getLogger(User.class); // static private final Logger logger = LoggerFactory.getLogger(User.class);
/* /*
* /usr/include/pwd.h * /usr/include/pwd.h
* *
* The passwd structure. * The passwd structure.
* struct passwd * struct passwd
* { * {
* char *pw_name;D Username. * char *pw_name;D Username.
* char *pw_passwd; Password. * char *pw_passwd; Password.
* __uid_t pw_uid; User ID. * __uid_t pw_uid; User ID.
* __gid_t pw_gid; Group ID. * __gid_t pw_gid; Group ID.
* char *pw_gecos; Real name. * char *pw_gecos; Real name.
* char *pw_dir; Home directory. * char *pw_dir; Home directory.
* char *pw_shell; Shell program. * char *pw_shell; Shell program.
* }; * };
*/ */
protected String login; protected String login;
protected String password; protected String password;
protected int uid; protected int uid;
protected int gid; protected int gid;
protected String realName; protected String realName;
protected String homeDirectory; protected String homeDirectory;
protected String shell; protected String shell;
/** /**
* *
*/ */
public User() public User()
{ {
this.login = null; this.login = null;
this.password = null; this.password = null;
this.uid = -1; this.uid = -1;
this.gid = -1; this.gid = -1;
this.realName = null; this.realName = null;
this.homeDirectory = null; this.homeDirectory = null;
this.shell = null; this.shell = null;
} }
/** /**
* *
*/ */
public User(final User user) public User(final User user)
{ {
this.login = user.login(); this.login = user.login();
this.password = user.passwd(); this.password = user.passwd();
this.uid = user.uid(); this.uid = user.uid();
this.gid = user.gid(); this.gid = user.gid();
this.realName = user.realName(); this.realName = user.realName();
this.homeDirectory = user.homeDirectory(); this.homeDirectory = user.homeDirectory();
this.shell = user.shell(); this.shell = user.shell();
} }
/** /**
* *
*/ */
public int getGid() public int getGid()
{ {
int result; int result;
result = this.gid; result = this.gid;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getHomeDirectory() public String getHomeDirectory()
{ {
String result; String result;
result = this.homeDirectory; result = this.homeDirectory;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getLogin() public String getLogin()
{ {
String result; String result;
result = this.login; result = this.login;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getPasswd() public String getPasswd()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getPassword() public String getPassword()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getRealName() public String getRealName()
{ {
String result; String result;
result = this.realName; result = this.realName;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String getShell() public String getShell()
{ {
String result; String result;
result = this.shell; result = this.shell;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int getUid() public int getUid()
{ {
int result; int result;
result = this.uid; result = this.uid;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int gid() public int gid()
{ {
int result; int result;
result = this.gid; result = this.gid;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String homeDirectory() public String homeDirectory()
{ {
String result; String result;
result = this.homeDirectory; result = this.homeDirectory;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String login() public String login()
{ {
String result; String result;
result = this.login; result = this.login;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String passwd() public String passwd()
{ {
String result; String result;
result = this.password; result = this.password;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String realName() public String realName()
{ {
String result; String result;
result = this.realName; result = this.realName;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void setGid(final int gid) public void setGid(final int gid)
{ {
this.gid = gid; this.gid = gid;
} }
/** /**
* *
*/ */
public void setHomeDirectory(final String homeDirectory) public void setHomeDirectory(final String homeDirectory)
{ {
this.homeDirectory = homeDirectory; this.homeDirectory = homeDirectory;
} }
/** /**
* *
*/ */
public void setLogin(final String login) public void setLogin(final String login)
{ {
this.login = login; this.login = login;
} }
/** /**
* *
*/ */
public void setPasswd(final String password) public void setPasswd(final String password)
{ {
this.password = password; this.password = password;
} }
/** /**
* *
*/ */
public void setPassword(final String password) public void setPassword(final String password)
{ {
this.password = password; this.password = password;
} }
/** /**
* *
*/ */
public void setRealName(final String realName) public void setRealName(final String realName)
{ {
this.realName = realName; this.realName = realName;
} }
/** /**
* *
*/ */
public void setShell(final String shell) public void setShell(final String shell)
{ {
this.shell = shell; this.shell = shell;
} }
/** /**
* *
*/ */
public void setUid(final int uid) public void setUid(final int uid)
{ {
this.uid = uid; this.uid = uid;
} }
/** /**
* *
*/ */
public String shell() public String shell()
{ {
String result; String result;
result = this.shell; result = this.shell;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = "|" + this.login + "|" + this.password + "|" + this.uid + "|" + this.gid + "|" + this.realName + "|" + this.homeDirectory + "|" + this.shell + "|"; result = "|" + this.login + "|" + this.password + "|" + this.uid + "|" + this.gid + "|" + this.realName + "|" + this.homeDirectory + "|" + this.shell + "|";
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public int uid() public int uid()
{ {
int result; int result;
result = this.uid; result = this.uid;
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,160 +1,160 @@
package fr.devinsy.util.unix; package fr.devinsy.util.unix;
import java.util.Iterator; import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
/** /**
* *
*/ */
public class Users extends Vector<User> public class Users extends Vector<User>
{ {
private static final long serialVersionUID = -7178304512851592399L; private static final long serialVersionUID = -7178304512851592399L;
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(Users.class); // LoggerFactory.getLogger(Users.class);
/** /**
* *
*/ */
public Users() public Users()
{ {
super(); super();
} }
/* /*
* *
*/ */
public boolean contains(final int uid) public boolean contains(final int uid)
{ {
boolean result; boolean result;
if (getByUid(uid) == null) if (getByUid(uid) == null)
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/* /*
* *
*/ */
public boolean contains(final String login) public boolean contains(final String login)
{ {
boolean result; boolean result;
if (getByLogin(login) == null) if (getByLogin(login) == null)
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public User getByLogin(final String login) public User getByLogin(final String login)
{ {
User result; User result;
if (login == null) if (login == null)
{ {
result = null; result = null;
} }
else else
{ {
result = null; result = null;
boolean ended = false; boolean ended = false;
Iterator<User> iterator = this.iterator(); Iterator<User> iterator = this.iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
{ {
User user = iterator.next(); User user = iterator.next();
if (user.getLogin().equals(login)) if (user.getLogin().equals(login))
{ {
ended = true; ended = true;
result = user; result = user;
} }
} }
else else
{ {
ended = true; ended = true;
result = null; result = null;
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public User getByUid(final int uid) public User getByUid(final int uid)
{ {
User result; User result;
result = null; result = null;
boolean ended = false; boolean ended = false;
Iterator<User> iterator = this.iterator(); Iterator<User> iterator = this.iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
{ {
User user = iterator.next(); User user = iterator.next();
if (user.getUid() == uid) if (user.getUid() == uid)
{ {
ended = true; ended = true;
result = user; result = user;
} }
} }
else else
{ {
ended = true; ended = true;
result = null; result = null;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
StringBuffer out; StringBuffer out;
out = new StringBuffer(); out = new StringBuffer();
Iterator<User> iterator = this.iterator(); Iterator<User> iterator = this.iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
out.append(iterator.next().toString() + "\n"); out.append(iterator.next().toString() + "\n");
} }
result = out.toString(); result = out.toString();
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,197 +1,197 @@
package fr.devinsy.util.unix.acl; package fr.devinsy.util.unix.acl;
/** /**
* *
*/ */
public class Acl public class Acl
{ {
// static private final Logger logger = LoggerFactory.getLogger(Acl.class); // static private final Logger logger = LoggerFactory.getLogger(Acl.class);
/* /*
# file: goo39 # file: goo39
# owner: goo39 # owner: goo39
# group: goo39 # group: goo39
user::rwx user::rwx
user:cpm:rwx #effective:rwx user:cpm:rwx #effective:rwx
user:goo39:rwx #effective:rwx user:goo39:rwx #effective:rwx
group::--- #effective:--- group::--- #effective:---
group:goo40:rwx #effective:rwx group:goo40:rwx #effective:rwx
mask::rwx mask::rwx
other::--- other::---
default:user::rwx default:user::rwx
default:user:cpm:rwx #effective:rwx default:user:cpm:rwx #effective:rwx
default:group::--- #effective:--- default:group::--- #effective:---
default:group:cpm:rwx #effective:rwx default:group:cpm:rwx #effective:rwx
default:group:goo40:rwx #effective:rwx default:group:goo40:rwx #effective:rwx
default:mask::rwx default:mask::rwx
default:other::--- default:other::---
*/ */
protected String filePathname; protected String filePathname;
protected String owner; protected String owner;
protected String group; protected String group;
protected AclEntries currentAcl; protected AclEntries currentAcl;
protected AclEntries defaultAcl; protected AclEntries defaultAcl;
/** /**
* *
*/ */
public Acl(final String filePathname) public Acl(final String filePathname)
{ {
this.filePathname = filePathname; this.filePathname = filePathname;
this.owner = ""; this.owner = "";
this.group = ""; this.group = "";
this.currentAcl = new AclEntries(); this.currentAcl = new AclEntries();
this.defaultAcl = new AclEntries(); this.defaultAcl = new AclEntries();
} }
/** /**
* *
*/ */
public boolean contains(final AclEntry.Type type, final String id) public boolean contains(final AclEntry.Type type, final String id)
{ {
boolean result; boolean result;
if ((this.currentAcl().contains(type, id)) || (this.defaultAcl().contains(type, id))) if ((this.currentAcl().contains(type, id)) || (this.defaultAcl().contains(type, id)))
{ {
result = true; result = true;
} }
else else
{ {
result = false; result = false;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public boolean containsGroup(final String group) public boolean containsGroup(final String group)
{ {
boolean result; boolean result;
result = contains(AclEntry.Type.GROUP, group); result = contains(AclEntry.Type.GROUP, group);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public boolean containsId(final String id) public boolean containsId(final String id)
{ {
boolean result; boolean result;
if ((containsUser(id) || containsGroup(id))) if ((containsUser(id) || containsGroup(id)))
{ {
result = true; result = true;
} }
else else
{ {
result = false; result = false;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public boolean containsUser(final String login) public boolean containsUser(final String login)
{ {
boolean result; boolean result;
result = contains(AclEntry.Type.USER, login); result = contains(AclEntry.Type.USER, login);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public AclEntries currentAcl() public AclEntries currentAcl()
{ {
AclEntries result; AclEntries result;
result = this.currentAcl; result = this.currentAcl;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public AclEntries defaultAcl() public AclEntries defaultAcl()
{ {
AclEntries result; AclEntries result;
result = this.defaultAcl; result = this.defaultAcl;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String filePathname() public String filePathname()
{ {
String result; String result;
result = this.filePathname; result = this.filePathname;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String group() public String group()
{ {
String result; String result;
result = this.group; result = this.group;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String owner() public String owner()
{ {
String result; String result;
result = this.owner; result = this.owner;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public void setGroup(final String group) public void setGroup(final String group)
{ {
this.group = group; this.group = group;
} }
/** /**
* *
*/ */
public void setOwner(final String owner) public void setOwner(final String owner)
{ {
this.owner = owner; this.owner = owner;
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,197 +1,197 @@
package fr.devinsy.util.unix.acl; package fr.devinsy.util.unix.acl;
import java.util.Iterator; import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
/** /**
* *
*/ */
public class AclEntries extends Vector<AclEntry> public class AclEntries extends Vector<AclEntry>
{ {
private static final long serialVersionUID = 5802487312198869603L; private static final long serialVersionUID = 5802487312198869603L;
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(AclEntries.class); // LoggerFactory.getLogger(AclEntries.class);
/** /**
* *
*/ */
public AclEntries() public AclEntries()
{ {
super(); super();
} }
/* /*
* *
*/ */
public boolean contains(final AclEntry.Type type, final String id) public boolean contains(final AclEntry.Type type, final String id)
{ {
boolean result; boolean result;
if (this.get(type, id) == null) if (this.get(type, id) == null)
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public boolean containsId(final String id) public boolean containsId(final String id)
{ {
boolean result; boolean result;
if ((this.get(AclEntry.Type.USER, id) == null) && (this.get(AclEntry.Type.GROUP, id) == null)) if ((this.get(AclEntry.Type.USER, id) == null) && (this.get(AclEntry.Type.GROUP, id) == null))
{ {
result = false; result = false;
} }
else else
{ {
result = true; result = true;
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public AclEntry get(final AclEntry.Type type, final String id) public AclEntry get(final AclEntry.Type type, final String id)
{ {
AclEntry result; AclEntry result;
result = null; result = null;
boolean ended = false; boolean ended = false;
Iterator<AclEntry> iterator = this.iterator(); Iterator<AclEntry> iterator = this.iterator();
while (!ended) while (!ended)
{ {
if (iterator.hasNext()) if (iterator.hasNext())
{ {
AclEntry entry = iterator.next(); AclEntry entry = iterator.next();
if ((type == entry.type()) && (entry.id().equals(id))) if ((type == entry.type()) && (entry.id().equals(id)))
{ {
ended = true; ended = true;
result = entry; result = entry;
} }
} }
else else
{ {
ended = true; ended = true;
result = null; result = null;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String[] getGroupIds() public String[] getGroupIds()
{ {
String[] result; String[] result;
Vector<String> ids = new Vector<String>(); Vector<String> ids = new Vector<String>();
for (AclEntry entry : this) for (AclEntry entry : this)
{ {
if ((entry.type == AclEntry.Type.GROUP) && (!ids.contains(entry.id()))) if ((entry.type == AclEntry.Type.GROUP) && (!ids.contains(entry.id())))
{ {
ids.add(entry.id()); ids.add(entry.id());
} }
} }
result = (String[]) ids.toArray(); result = (String[]) ids.toArray();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String[] getIds() public String[] getIds()
{ {
String[] result; String[] result;
Vector<String> ids = new Vector<String>(); Vector<String> ids = new Vector<String>();
for (AclEntry entry : this) for (AclEntry entry : this)
{ {
if (!ids.contains(entry.id())) if (!ids.contains(entry.id()))
{ {
ids.add(entry.id()); ids.add(entry.id());
} }
} }
result = (String[]) ids.toArray(); result = (String[]) ids.toArray();
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String[] getUserIds() public String[] getUserIds()
{ {
String[] result; String[] result;
// //
Vector<String> ids = new Vector<String>(); Vector<String> ids = new Vector<String>();
for (AclEntry entry : this) for (AclEntry entry : this)
{ {
if ((entry.type == AclEntry.Type.USER) && (!ids.contains(entry.id()))) if ((entry.type == AclEntry.Type.USER) && (!ids.contains(entry.id())))
{ {
ids.add(entry.id()); ids.add(entry.id());
} }
} }
// //
result = new String[ids.size()]; result = new String[ids.size()];
for (int nId = 0; nId < ids.size(); nId++) for (int nId = 0; nId < ids.size(); nId++)
{ {
result[nId] = ids.get(nId); result[nId] = ids.get(nId);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
StringBuffer out; StringBuffer out;
out = new StringBuffer(); out = new StringBuffer();
Iterator<AclEntry> iterator = this.iterator(); Iterator<AclEntry> iterator = this.iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
out.append(iterator.next().toString() + "\n"); out.append(iterator.next().toString() + "\n");
} }
result = out.toString(); result = out.toString();
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,95 +1,95 @@
package fr.devinsy.util.unix.acl; package fr.devinsy.util.unix.acl;
/** /**
* *
*/ */
public class AclEntry public class AclEntry
{ {
// static private final Logger logger = // static private final Logger logger =
// LoggerFactory.getLogger(AclEntry.class); // LoggerFactory.getLogger(AclEntry.class);
/* /*
user::rwx user::rwx
user:cpm:rwx #effective:rwx user:cpm:rwx #effective:rwx
user:goo39:rwx #effective:rwx user:goo39:rwx #effective:rwx
group::--- #effective:--- group::--- #effective:---
group:goo40:rwx #effective:rwx group:goo40:rwx #effective:rwx
mask::rwx mask::rwx
other::--- other::---
*/ */
public enum Type public enum Type
{ {
NONE, USER, GROUP, MASK, OTHER NONE, USER, GROUP, MASK, OTHER
}; };
protected Type type; protected Type type;
protected String id; protected String id;
protected String permission; protected String permission;
/** /**
* *
*/ */
public AclEntry(final Type type, final String id, final String permission) public AclEntry(final Type type, final String id, final String permission)
{ {
this.type = type; this.type = type;
this.id = id; this.id = id;
this.permission = permission; this.permission = permission;
} }
/** /**
* *
*/ */
public String id() public String id()
{ {
String result; String result;
result = this.id; result = this.id;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public String permission() public String permission()
{ {
String result; String result;
result = this.type.toString() + ":" + this.id + ":" + this.permission; result = this.type.toString() + ":" + this.id + ":" + this.permission;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
@Override @Override
public String toString() public String toString()
{ {
String result; String result;
result = permission; result = permission;
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public Type type() public Type type()
{ {
Type result; Type result;
result = this.type; result = this.type;
// //
return (result); return (result);
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////

View file

@ -1,405 +1,405 @@
package fr.devinsy.util.unix.acl; package fr.devinsy.util.unix.acl;
import java.io.File; import java.io.File;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringConcatenator; import fr.devinsy.util.StringConcatenator;
import fr.devinsy.util.cmdexec.CmdExec; import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.unix.Unix; import fr.devinsy.util.unix.Unix;
/** /**
* *
*/ */
public class AclManager public class AclManager
{ {
static private final Logger logger = LoggerFactory.getLogger(AclManager.class); static private final Logger logger = LoggerFactory.getLogger(AclManager.class);
static public final String SUDO = "/usr/bin/sudo"; static public final String SUDO = "/usr/bin/sudo";
static final public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$"); static final public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$");
static final public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$"); static final public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$");
static final public Pattern MASK_PATTERN = Pattern.compile("^mask:(.*):(.*)$"); static final public Pattern MASK_PATTERN = Pattern.compile("^mask:(.*):(.*)$");
static final public Pattern OTHER_PATTERN = Pattern.compile("^other:(.*):(.*)$"); static final public Pattern OTHER_PATTERN = Pattern.compile("^other:(.*):(.*)$");
static final public Pattern DEFAULT_USER_PATTERN = Pattern.compile("^default:user:(.*):(.*)$"); static final public Pattern DEFAULT_USER_PATTERN = Pattern.compile("^default:user:(.*):(.*)$");
static final public Pattern DEFAULT_GROUP_PATTERN = Pattern.compile("^default:group:(.*):(.*)$"); static final public Pattern DEFAULT_GROUP_PATTERN = Pattern.compile("^default:group:(.*):(.*)$");
static final public Pattern DEFAULT_MASK_PATTERN = Pattern.compile("^default:mask:(.*):(.*)$"); static final public Pattern DEFAULT_MASK_PATTERN = Pattern.compile("^default:mask:(.*):(.*)$");
static final public Pattern DEFAULT_OTHER_PATTERN = Pattern.compile("^default:other:(.*):(.*)$"); static final public Pattern DEFAULT_OTHER_PATTERN = Pattern.compile("^default:other:(.*):(.*)$");
/** /**
* *
*/ */
public static void clearGroup(final String group, final String filePathName) throws Exception public static void clearGroup(final String group, final String filePathName) throws Exception
{ {
if ((group == null) || (group.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) if ((group == null) || (group.length() == 0) || (filePathName == null) || (filePathName.length() == 0))
{ {
throw new Exception("Bad parameters [" + group + "][" + filePathName + "]."); throw new Exception("Bad parameters [" + group + "][" + filePathName + "].");
} }
else else
{ {
Unix.setfacl("-R", "-L", "-x", "group:" + group, filePathName); Unix.setfacl("-R", "-L", "-x", "group:" + group, filePathName);
Unix.setfacl("-R", "-L", "-d", "-x", "group:" + group, filePathName); Unix.setfacl("-R", "-L", "-d", "-x", "group:" + group, filePathName);
} }
} }
/** /**
* *
*/ */
public static void clearId(final String id, final String filePathName) throws Exception public static void clearId(final String id, final String filePathName) throws Exception
{ {
clearUser(id, filePathName); clearUser(id, filePathName);
clearGroup(id, filePathName); clearGroup(id, filePathName);
} }
/** /**
* *
*/ */
public static void clearUser(final String login, final String filePathName) throws Exception public static void clearUser(final String login, final String filePathName) throws Exception
{ {
if ((login == null) || (login.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) if ((login == null) || (login.length() == 0) || (filePathName == null) || (filePathName.length() == 0))
{ {
throw new Exception("Bad parameters [" + login + "][" + filePathName + "]."); throw new Exception("Bad parameters [" + login + "][" + filePathName + "].");
} }
else else
{ {
Unix.setfacl("-R", "-L", "-x", "user:" + login, filePathName); Unix.setfacl("-R", "-L", "-x", "user:" + login, filePathName);
Unix.setfacl("-R", "-L", "-d", "-x", "user:" + login, filePathName); Unix.setfacl("-R", "-L", "-d", "-x", "user:" + login, filePathName);
} }
} }
/** /**
* *
*/ */
static public Acl getAcl(final String filePathName) throws Exception static public Acl getAcl(final String filePathName) throws Exception
{ {
Acl result; Acl result;
result = new Acl(filePathName); result = new Acl(filePathName);
String[] entries = getAclEntryLines(filePathName); String[] entries = getAclEntryLines(filePathName);
// Login pattern: "^[a-z_][a-z0-9_-]*$". // Login pattern: "^[a-z_][a-z0-9_-]*$".
logger.debug("Line=[" + entries[1] + "]"); logger.debug("Line=[" + entries[1] + "]");
Matcher matcher = Pattern.compile("^#\\sowner:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[1]); Matcher matcher = Pattern.compile("^#\\sowner:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[1]);
if (matcher.matches()) if (matcher.matches())
{ {
logger.debug("group=[" + matcher.group(1) + "]"); logger.debug("group=[" + matcher.group(1) + "]");
result.setOwner(matcher.group(1)); result.setOwner(matcher.group(1));
} }
// Group pattern: "^[a-z_][a-z0-9_-]*$". // Group pattern: "^[a-z_][a-z0-9_-]*$".
logger.debug("Line=[" + entries[2] + "]"); logger.debug("Line=[" + entries[2] + "]");
matcher = Pattern.compile("^#\\sgroup:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[2]); matcher = Pattern.compile("^#\\sgroup:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[2]);
if (matcher.matches()) if (matcher.matches())
{ {
logger.debug("group=[" + matcher.group(1) + "]"); logger.debug("group=[" + matcher.group(1) + "]");
result.setOwner(matcher.group(1)); result.setOwner(matcher.group(1));
} }
for (int nEntry = 3; nEntry < entries.length; nEntry++) for (int nEntry = 3; nEntry < entries.length; nEntry++)
{ {
String entryLine = entries[nEntry]; String entryLine = entries[nEntry];
logger.debug("Line=[" + entryLine + "]"); logger.debug("Line=[" + entryLine + "]");
// //
Matcher userMatcher = USER_PATTERN.matcher(entryLine); Matcher userMatcher = USER_PATTERN.matcher(entryLine);
Matcher groupMatcher = GROUP_PATTERN.matcher(entryLine); Matcher groupMatcher = GROUP_PATTERN.matcher(entryLine);
Matcher maskMatcher = MASK_PATTERN.matcher(entryLine); Matcher maskMatcher = MASK_PATTERN.matcher(entryLine);
Matcher otherMatcher = OTHER_PATTERN.matcher(entryLine); Matcher otherMatcher = OTHER_PATTERN.matcher(entryLine);
Matcher defaultUserMatcher = DEFAULT_USER_PATTERN.matcher(entryLine); Matcher defaultUserMatcher = DEFAULT_USER_PATTERN.matcher(entryLine);
Matcher defaultGroupMatcher = DEFAULT_GROUP_PATTERN.matcher(entryLine); Matcher defaultGroupMatcher = DEFAULT_GROUP_PATTERN.matcher(entryLine);
Matcher defaultMaskMatcher = DEFAULT_MASK_PATTERN.matcher(entryLine); Matcher defaultMaskMatcher = DEFAULT_MASK_PATTERN.matcher(entryLine);
Matcher defaultOtherMatcher = DEFAULT_OTHER_PATTERN.matcher(entryLine); Matcher defaultOtherMatcher = DEFAULT_OTHER_PATTERN.matcher(entryLine);
AclEntry entry; AclEntry entry;
if (userMatcher.matches()) if (userMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.USER, userMatcher.group(1), userMatcher.group(2)); entry = new AclEntry(AclEntry.Type.USER, userMatcher.group(1), userMatcher.group(2));
result.currentAcl().add(entry); result.currentAcl().add(entry);
} }
else if (groupMatcher.matches()) else if (groupMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.GROUP, groupMatcher.group(1), groupMatcher.group(2)); entry = new AclEntry(AclEntry.Type.GROUP, groupMatcher.group(1), groupMatcher.group(2));
result.currentAcl().add(entry); result.currentAcl().add(entry);
} }
else if (maskMatcher.matches()) else if (maskMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.MASK, maskMatcher.group(1), maskMatcher.group(2)); entry = new AclEntry(AclEntry.Type.MASK, maskMatcher.group(1), maskMatcher.group(2));
result.currentAcl().add(entry); result.currentAcl().add(entry);
} }
else if (otherMatcher.matches()) else if (otherMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.OTHER, otherMatcher.group(1), otherMatcher.group(2)); entry = new AclEntry(AclEntry.Type.OTHER, otherMatcher.group(1), otherMatcher.group(2));
result.currentAcl().add(entry); result.currentAcl().add(entry);
} }
else if (defaultUserMatcher.matches()) else if (defaultUserMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.USER, defaultUserMatcher.group(1), defaultUserMatcher.group(2)); entry = new AclEntry(AclEntry.Type.USER, defaultUserMatcher.group(1), defaultUserMatcher.group(2));
result.defaultAcl().add(entry); result.defaultAcl().add(entry);
} }
else if (defaultGroupMatcher.matches()) else if (defaultGroupMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.GROUP, defaultGroupMatcher.group(1), defaultGroupMatcher.group(2)); entry = new AclEntry(AclEntry.Type.GROUP, defaultGroupMatcher.group(1), defaultGroupMatcher.group(2));
result.defaultAcl().add(entry); result.defaultAcl().add(entry);
} }
else if (defaultMaskMatcher.matches()) else if (defaultMaskMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.MASK, defaultMaskMatcher.group(1), defaultMaskMatcher.group(2)); entry = new AclEntry(AclEntry.Type.MASK, defaultMaskMatcher.group(1), defaultMaskMatcher.group(2));
result.defaultAcl().add(entry); result.defaultAcl().add(entry);
} }
else if (defaultOtherMatcher.matches()) else if (defaultOtherMatcher.matches())
{ {
entry = new AclEntry(AclEntry.Type.OTHER, defaultOtherMatcher.group(1), defaultOtherMatcher.group(2)); entry = new AclEntry(AclEntry.Type.OTHER, defaultOtherMatcher.group(1), defaultOtherMatcher.group(2));
result.defaultAcl().add(entry); result.defaultAcl().add(entry);
} }
else else
{ {
throw new Exception("Unknow ACL entry line pattern for [" + entryLine + "]."); throw new Exception("Unknow ACL entry line pattern for [" + entryLine + "].");
} }
logger.debug("Acl entry decoded: [" + entry.toString() + "]"); logger.debug("Acl entry decoded: [" + entry.toString() + "]");
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String getAclData(final String filePathName) throws Exception static public String getAclData(final String filePathName) throws Exception
{ {
String result; String result;
try try
{ {
logger.info("Getting Acl data for [" + filePathName + "]."); logger.info("Getting Acl data for [" + filePathName + "].");
result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName); result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName);
logger.info("Acl data got for [" + filePathName + "]."); logger.info("Acl data got for [" + filePathName + "].");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception("Error getting ACL for [" + filePathName + "].", exception); throw new Exception("Error getting ACL for [" + filePathName + "].", exception);
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public String[] getAclEntryLines(final String filePathName) static public String[] getAclEntryLines(final String filePathName)
{ {
String[] result; String[] result;
try try
{ {
result = getAclData(filePathName).split("\n"); result = getAclData(filePathName).split("\n");
} }
catch (Exception exception) catch (Exception exception)
{ {
result = new String[0]; result = new String[0];
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws Exception public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws Exception
{ {
boolean result; boolean result;
result = isUsed(type, id, filePathName, 0); result = isUsed(type, id, filePathName, 0);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws Exception public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws Exception
{ {
boolean result; boolean result;
if ((type == null) || (id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) if ((type == null) || (id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0))
{ {
throw new Exception("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "]."); throw new Exception("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "].");
} }
else else
{ {
File file = new File(filePathName); File file = new File(filePathName);
if (!file.exists()) if (!file.exists())
{ {
throw new Exception("File does not exist [" + filePathName + "]."); throw new Exception("File does not exist [" + filePathName + "].");
} }
else else
{ {
Acl acl = getAcl(filePathName); Acl acl = getAcl(filePathName);
if (acl.contains(type, id)) if (acl.contains(type, id))
{ {
result = true; result = true;
} }
else if ((file.isDirectory()) && (depth > 0)) else if ((file.isDirectory()) && (depth > 0))
{ {
result = isUsed(type, id, filePathName, file.list(), depth - 1); result = isUsed(type, id, filePathName, file.list(), depth - 1);
} }
else else
{ {
result = false; result = false;
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws Exception public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws Exception
{ {
boolean result; boolean result;
result = false; result = false;
boolean ended = false; boolean ended = false;
int nLine = 0; int nLine = 0;
while (!ended) while (!ended)
{ {
if (nLine < filePathNames.length) if (nLine < filePathNames.length)
{ {
String filePathName = filePathNames[nLine]; String filePathName = filePathNames[nLine];
if (isUsed(type, id, filePath + "/" + filePathName, depth)) if (isUsed(type, id, filePath + "/" + filePathName, depth))
{ {
ended = true; ended = true;
result = true; result = true;
} }
else else
{ {
nLine += 1; nLine += 1;
} }
} }
else else
{ {
ended = true; ended = true;
result = false; result = false;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final String id, final String filePathName) throws Exception public static boolean isUsed(final String id, final String filePathName) throws Exception
{ {
boolean result; boolean result;
result = isUsed(id, filePathName, 0); result = isUsed(id, filePathName, 0);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final String id, final String filePathName, final int depth) throws Exception public static boolean isUsed(final String id, final String filePathName, final int depth) throws Exception
{ {
boolean result; boolean result;
if ((id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) if ((id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0))
{ {
throw new Exception("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "]."); throw new Exception("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "].");
} }
else else
{ {
File file = new File(filePathName); File file = new File(filePathName);
if (!file.exists()) if (!file.exists())
{ {
throw new Exception("File does not exist [" + filePathName + "]."); throw new Exception("File does not exist [" + filePathName + "].");
} }
else else
{ {
Acl acl = getAcl(filePathName); Acl acl = getAcl(filePathName);
if (acl.containsId(id)) if (acl.containsId(id))
{ {
result = true; result = true;
} }
else if ((file.isDirectory()) && (depth > 0)) else if ((file.isDirectory()) && (depth > 0))
{ {
result = isUsed(id, file.list(), depth - 1); result = isUsed(id, file.list(), depth - 1);
} }
else else
{ {
result = false; result = false;
} }
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws Exception public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws Exception
{ {
boolean result; boolean result;
result = false; result = false;
boolean ended = false; boolean ended = false;
int nLine = 0; int nLine = 0;
while (!ended) while (!ended)
{ {
if (nLine < filePathNames.length) if (nLine < filePathNames.length)
{ {
String filePathName = filePathNames[nLine]; String filePathName = filePathNames[nLine];
if (isUsed(id, filePathName, depth)) if (isUsed(id, filePathName, depth))
{ {
ended = true; ended = true;
result = true; result = true;
} }
else else
{ {
nLine += 1; nLine += 1;
} }
} }
else else
{ {
ended = true; ended = true;
result = false; result = false;
} }
} }
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public void setfacl(final String... args) throws Exception static public void setfacl(final String... args) throws Exception
{ {
try try
{ {
CmdExec.run(SUDO, "setfacl", args, 1, 5); CmdExec.run(SUDO, "setfacl", args, 1, 5);
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + "."); throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".");
} }
} }
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////