From 16c24fa114299c75f204cab72d24067c8df43021 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Fri, 28 Jun 2013 14:40:16 +0200 Subject: [PATCH] dos2unix --- src/META-INF/MANIFEST.MF | 6 +- src/fr/devinsy/util/DataFile.java | 484 +++---- src/fr/devinsy/util/DataFiles.java | 334 ++--- src/fr/devinsy/util/DateHelper.java | 714 +++++----- src/fr/devinsy/util/DateTimeHelper.java | 222 ++-- .../util/InternetProxyConfiguration.java | 456 +++---- src/fr/devinsy/util/SimpleAveragemeter.java | 240 ++-- src/fr/devinsy/util/SimpleChronometer.java | 302 ++--- src/fr/devinsy/util/StringConcatenator.java | 766 +++++------ src/fr/devinsy/util/StringList.java | 1028 +++++++-------- src/fr/devinsy/util/StringListWriter.java | 154 +-- src/fr/devinsy/util/cmdexec/Wrapper.java | 132 +- src/fr/devinsy/util/unix/CachedFile.java | 228 ++-- src/fr/devinsy/util/unix/EtcGroupFile.java | 360 ++--- .../devinsy/util/unix/EtcGroupFileReader.java | 164 +-- src/fr/devinsy/util/unix/EtcPasswdFile.java | 332 ++--- .../util/unix/EtcPasswdFileReader.java | 170 +-- src/fr/devinsy/util/unix/Group.java | 406 +++--- src/fr/devinsy/util/unix/Groups.java | 426 +++--- src/fr/devinsy/util/unix/Unix.java | 1154 ++++++++--------- src/fr/devinsy/util/unix/User.java | 672 +++++----- src/fr/devinsy/util/unix/Users.java | 318 ++--- src/fr/devinsy/util/unix/acl/Acl.java | 392 +++--- src/fr/devinsy/util/unix/acl/AclEntries.java | 392 +++--- src/fr/devinsy/util/unix/acl/AclEntry.java | 188 +-- src/fr/devinsy/util/unix/acl/AclManager.java | 808 ++++++------ 26 files changed, 5424 insertions(+), 5424 deletions(-) diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF index 5e94951..254272e 100644 --- a/src/META-INF/MANIFEST.MF +++ b/src/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ -Manifest-Version: 1.0 -Class-Path: - +Manifest-Version: 1.0 +Class-Path: + diff --git a/src/fr/devinsy/util/DataFile.java b/src/fr/devinsy/util/DataFile.java index 39c196d..6c101c8 100755 --- a/src/fr/devinsy/util/DataFile.java +++ b/src/fr/devinsy/util/DataFile.java @@ -1,242 +1,242 @@ -/** - * @author Christian Momon, June 2008. - * 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. - */ -package fr.devinsy.util; - -/** - * This class defines a content file. - * - */ -public class DataFile -{ - static public int NOID = 0; - static public int DEFAULT_SIZE = 0; - - // - protected int id; - protected int contentId; - protected String name; - protected long size; - protected byte[] data; - protected String creationDate; - protected String creationUser; - - /** - * - */ - public DataFile() - { - this.id = NOID; - this.contentId = NOID; - this.name = null; - this.size = DEFAULT_SIZE; - this.data = null; - this.creationDate = null; - this.creationUser = null; - } - - /** - * - */ - public DataFile(final int contentId, final String name, final long size, final byte[] data) - { - this.id = NOID; - this.contentId = contentId; - this.name = name; - this.size = size; - this.data = data; - this.creationDate = null; - this.creationUser = null; - } - - /** - * - */ - public DataFile(final String name, final long size, final byte[] data) - { - this.id = NOID; - this.contentId = NOID; - this.name = name; - this.size = size; - this.data = data; - this.creationDate = null; - this.creationUser = null; - } - - /** - * - */ - public int contentId() - { - int result; - - result = this.contentId; - - // - return (result); - } - - /** - * - */ - public String creationDate() - { - String result; - - result = this.creationDate; - - // - return (result); - } - - /** - * - */ - public String creationUser() - { - String result; - - result = this.creationUser; - - // - return (result); - } - - /** - * - */ - public byte[] data() - { - byte[] result; - - result = this.data; - - // - return (result); - } - - /** - * - */ - public int id() - { - int result; - - result = this.id; - - // - return (result); - } - - /** - * - */ - public String name() - { - String result; - - result = this.name; - - // - return (result); - } - - /** - * - */ - public void setContentId(final int contentId) - { - this.contentId = contentId; - } - - /** - * - */ - public void setCreationDate(final String creationDate) - { - if (creationDate == null) - { - this.creationDate = ""; - } - else - { - this.creationDate = creationDate; - } - } - - /** - * - */ - public void setCreationUser(final String creationUser) - { - if (creationUser == null) - { - this.creationUser = ""; - } - else - { - this.creationUser = creationUser; - } - } - - /** - * - */ - public void setData(final byte[] data) - { - this.data = data; - } - - /** - * - */ - public void setId(final int id) - { - this.id = id; - } - - /** - * - */ - public void setName(final String name) - { - if (name == null) - { - this.name = ""; - } - else - { - this.name = name; - } - } - - /** - * - */ - public void setSize(final long size) - { - if (size >= 0) - { - this.size = size; - } - else - { - this.size = 0; - } - } - - /** - * - */ - public long size() - { - long result; - - result = this.size; - - // - return (result); - } -} +/** + * @author Christian Momon, June 2008. + * 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. + */ +package fr.devinsy.util; + +/** + * This class defines a content file. + * + */ +public class DataFile +{ + static public int NOID = 0; + static public int DEFAULT_SIZE = 0; + + // + protected int id; + protected int contentId; + protected String name; + protected long size; + protected byte[] data; + protected String creationDate; + protected String creationUser; + + /** + * + */ + public DataFile() + { + this.id = NOID; + this.contentId = NOID; + this.name = null; + this.size = DEFAULT_SIZE; + this.data = null; + this.creationDate = null; + this.creationUser = null; + } + + /** + * + */ + public DataFile(final int contentId, final String name, final long size, final byte[] data) + { + this.id = NOID; + this.contentId = contentId; + this.name = name; + this.size = size; + this.data = data; + this.creationDate = null; + this.creationUser = null; + } + + /** + * + */ + public DataFile(final String name, final long size, final byte[] data) + { + this.id = NOID; + this.contentId = NOID; + this.name = name; + this.size = size; + this.data = data; + this.creationDate = null; + this.creationUser = null; + } + + /** + * + */ + public int contentId() + { + int result; + + result = this.contentId; + + // + return (result); + } + + /** + * + */ + public String creationDate() + { + String result; + + result = this.creationDate; + + // + return (result); + } + + /** + * + */ + public String creationUser() + { + String result; + + result = this.creationUser; + + // + return (result); + } + + /** + * + */ + public byte[] data() + { + byte[] result; + + result = this.data; + + // + return (result); + } + + /** + * + */ + public int id() + { + int result; + + result = this.id; + + // + return (result); + } + + /** + * + */ + public String name() + { + String result; + + result = this.name; + + // + return (result); + } + + /** + * + */ + public void setContentId(final int contentId) + { + this.contentId = contentId; + } + + /** + * + */ + public void setCreationDate(final String creationDate) + { + if (creationDate == null) + { + this.creationDate = ""; + } + else + { + this.creationDate = creationDate; + } + } + + /** + * + */ + public void setCreationUser(final String creationUser) + { + if (creationUser == null) + { + this.creationUser = ""; + } + else + { + this.creationUser = creationUser; + } + } + + /** + * + */ + public void setData(final byte[] data) + { + this.data = data; + } + + /** + * + */ + public void setId(final int id) + { + this.id = id; + } + + /** + * + */ + public void setName(final String name) + { + if (name == null) + { + this.name = ""; + } + else + { + this.name = name; + } + } + + /** + * + */ + public void setSize(final long size) + { + if (size >= 0) + { + this.size = size; + } + else + { + this.size = 0; + } + } + + /** + * + */ + public long size() + { + long result; + + result = this.size; + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/DataFiles.java b/src/fr/devinsy/util/DataFiles.java index 538ecb2..e4d380d 100755 --- a/src/fr/devinsy/util/DataFiles.java +++ b/src/fr/devinsy/util/DataFiles.java @@ -1,167 +1,167 @@ -/** - * @author Christian Momon, June 2008. - * 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. - */ -package fr.devinsy.util; - -import java.util.ArrayList; - -/** - * This class is a collection of DataFile objects whit some specific methods. - * - */ -public class DataFiles extends ArrayList -{ - private static final long serialVersionUID = -4584622422555785456L; - - /** - * - * - */ - public DataFiles() - { - super(); - } - - /** - * - * @param source - */ - public DataFiles(final DataFiles source) - { - super(source); - } - - /** - * - */ - public DataFiles getByContentId(final int id) - { - DataFiles result = new DataFiles(); - - for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) - { - DataFile contentFile = this.getByIndex(nDataFile); - - if (contentFile.contentId() == id) - { - result.add(contentFile); - } - } - - // - return (result); - } - - /** - * - */ - public DataFile getById(final int id) - { - DataFile result = null; - - boolean ended = false; - int nDataFile = 0; - while (!ended) - { - if (nDataFile >= this.size()) - { - ended = true; - result = null; - } - else - { - DataFile contentFile = this.getByIndex(nDataFile); - - if (id == contentFile.id()) - { - ended = true; - result = contentFile; - } - else - { - nDataFile += 1; - } - } - } - - // - return (result); - } - - /** - * - */ - public DataFile getByIndex(final int index) - { - DataFile result; - - result = super.get(index); - - // - return (result); - } - - /** - * - */ - public DataFile getByName(final String name) - { - DataFile result = null; - - if ((name == null) || (name.equals(""))) - { - result = null; - } - else - { - boolean ended = false; - int dataFileIndex = 0; - while (!ended) - { - if (dataFileIndex >= this.size()) - { - ended = true; - result = null; - } - else - { - DataFile contentFile = this.getByIndex(dataFileIndex); - - if (name.equals(contentFile.name())) - { - ended = true; - result = contentFile; - } - else - { - dataFileIndex += 1; - } - } - } - } - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - StringBuffer result = new StringBuffer(); - - for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) - { - DataFile contentFile = this.getByIndex(nDataFile); - result.append("== " + contentFile.name() + "\n"); - result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n"); - } - - // - return (result.toString()); - } -} +/** + * @author Christian Momon, June 2008. + * 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. + */ +package fr.devinsy.util; + +import java.util.ArrayList; + +/** + * This class is a collection of DataFile objects whit some specific methods. + * + */ +public class DataFiles extends ArrayList +{ + private static final long serialVersionUID = -4584622422555785456L; + + /** + * + * + */ + public DataFiles() + { + super(); + } + + /** + * + * @param source + */ + public DataFiles(final DataFiles source) + { + super(source); + } + + /** + * + */ + public DataFiles getByContentId(final int id) + { + DataFiles result = new DataFiles(); + + for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) + { + DataFile contentFile = this.getByIndex(nDataFile); + + if (contentFile.contentId() == id) + { + result.add(contentFile); + } + } + + // + return (result); + } + + /** + * + */ + public DataFile getById(final int id) + { + DataFile result = null; + + boolean ended = false; + int nDataFile = 0; + while (!ended) + { + if (nDataFile >= this.size()) + { + ended = true; + result = null; + } + else + { + DataFile contentFile = this.getByIndex(nDataFile); + + if (id == contentFile.id()) + { + ended = true; + result = contentFile; + } + else + { + nDataFile += 1; + } + } + } + + // + return (result); + } + + /** + * + */ + public DataFile getByIndex(final int index) + { + DataFile result; + + result = super.get(index); + + // + return (result); + } + + /** + * + */ + public DataFile getByName(final String name) + { + DataFile result = null; + + if ((name == null) || (name.equals(""))) + { + result = null; + } + else + { + boolean ended = false; + int dataFileIndex = 0; + while (!ended) + { + if (dataFileIndex >= this.size()) + { + ended = true; + result = null; + } + else + { + DataFile contentFile = this.getByIndex(dataFileIndex); + + if (name.equals(contentFile.name())) + { + ended = true; + result = contentFile; + } + else + { + dataFileIndex += 1; + } + } + } + } + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + StringBuffer result = new StringBuffer(); + + for (int nDataFile = 0; nDataFile < this.size(); nDataFile++) + { + DataFile contentFile = this.getByIndex(nDataFile); + result.append("== " + contentFile.name() + "\n"); + result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n"); + } + + // + return (result.toString()); + } +} diff --git a/src/fr/devinsy/util/DateHelper.java b/src/fr/devinsy/util/DateHelper.java index 8bd6c39..a298c82 100755 --- a/src/fr/devinsy/util/DateHelper.java +++ b/src/fr/devinsy/util/DateHelper.java @@ -1,357 +1,357 @@ -/** - * @author Christian Momon, June 2010. - * 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. - */ -package fr.devinsy.util; - -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * This class groups function to help in Calendar manipulation. - * - * SimpleDateFormat is not used cause does not thread safe? - */ -public class DateHelper -{ - // static private final Logger logger = - // LoggerFactory.getLogger(DateHelper.class); - - static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d"; - static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d"; - static final String RAW_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 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 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 public String americanFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - else - { - 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); - } - - /** - * - */ - static public String europeanFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - else - { - result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR)); - } - - // - return (result); - } - - /** - * - */ - static public boolean isAmericanFormat(final String date) - { - boolean result; - - if (date == null) - { - result = false; - } - else - { - result = date.matches(AMERICAN_DATE_PATTERN); - } - - // - return (result); - } - - /** - * - */ - static public boolean isEuropeanFormat(final String date) - { - boolean result; - - if (date == null) - { - result = false; - } - else - { - result = date.matches(EUROPEAN_DATE_PATTERN); - } - - // - return (result); - } - - /** - * - */ - static public boolean isISOFormat(final String date) - { - boolean result; - - if (date == null) - { - result = false; - } - else - { - result = date.matches(ISO_DATE_PATTERN); - } - - // - return (result); - } - - /** - * - */ - static public String ISOFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - else - { - result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH)); - } - - // - return (result); - } - - /** - * - */ - static public boolean isRawFormat(final String date) - { - boolean result; - - if (date == null) - { - result = false; - } - else - { - result = date.matches(RAW_DATE_PATTERN); - } - - // - return (result); - } - - /** - * - */ - static public boolean isValidDate(final String date) - { - boolean result; - - if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date))) - { - result = true; - } - else - { - result = false; - } - - // - return (result); - } - - /** - * - */ - static public Calendar parseAmericanDate(final String date) - { - Calendar result; - - Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN); - Matcher matcher = pattern.matcher(date); - - 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))); - } - else - { - result = null; - } - - // - return (result); - } - - /** - * Note: European parsing test made before the American parsing one. - */ - static public Calendar parseDate(final String date) - { - Calendar result; - - if (isEuropeanFormat(date)) - { - result = parseEuropeanDate(date); - } - else if (isRawFormat(date)) - { - result = parseRawDate(date); - } - else if (isISOFormat(date)) - { - result = parseISODate(date); - } - else if (isAmericanFormat(date)) - { - result = parseAmericanDate(date); - } - else - { - result = null; - } - - // - return (result); - } - - /** - * - */ - static public Calendar parseEuropeanDate(final String date) - { - Calendar result; - - Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN); - Matcher matcher = pattern.matcher(date); - - 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))); - } - else - { - result = null; - } - - // - return (result); - } - - /** - * - */ - static public Calendar parseISODate(final String date) - { - Calendar result; - - Pattern pattern = Pattern.compile(ISO_DATE_PATTERN); - Matcher matcher = pattern.matcher(date); - - 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))); - } - else - { - result = null; - } - - // - return (result); - } - - /** - * - */ - static public Calendar parseRawDate(final String date) - { - Calendar result; - - Pattern pattern = Pattern.compile(RAW_DATE_PATTERN); - Matcher matcher = pattern.matcher(date); - - 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))); - } - else - { - result = null; - } - - // - return (result); - } - - /** - * - */ - static public String rawFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - else - { - result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1); - } - - // - return (result); - } - - /** - * - */ - static public String shortEuropeanFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - else - { - result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1); - } - - // - return (result); - } -} +/** + * @author Christian Momon, June 2010. + * 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. + */ +package fr.devinsy.util; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * This class groups function to help in Calendar manipulation. + * + * SimpleDateFormat is not used cause does not thread safe? + */ +public class DateHelper +{ + // static private final Logger logger = + // LoggerFactory.getLogger(DateHelper.class); + + static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d"; + static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d"; + static final String RAW_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 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 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 public String americanFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + 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); + } + + /** + * + */ + static public String europeanFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR)); + } + + // + return (result); + } + + /** + * + */ + static public boolean isAmericanFormat(final String date) + { + boolean result; + + if (date == null) + { + result = false; + } + else + { + result = date.matches(AMERICAN_DATE_PATTERN); + } + + // + return (result); + } + + /** + * + */ + static public boolean isEuropeanFormat(final String date) + { + boolean result; + + if (date == null) + { + result = false; + } + else + { + result = date.matches(EUROPEAN_DATE_PATTERN); + } + + // + return (result); + } + + /** + * + */ + static public boolean isISOFormat(final String date) + { + boolean result; + + if (date == null) + { + result = false; + } + else + { + result = date.matches(ISO_DATE_PATTERN); + } + + // + return (result); + } + + /** + * + */ + static public String ISOFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH)); + } + + // + return (result); + } + + /** + * + */ + static public boolean isRawFormat(final String date) + { + boolean result; + + if (date == null) + { + result = false; + } + else + { + result = date.matches(RAW_DATE_PATTERN); + } + + // + return (result); + } + + /** + * + */ + static public boolean isValidDate(final String date) + { + boolean result; + + if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date))) + { + result = true; + } + else + { + result = false; + } + + // + return (result); + } + + /** + * + */ + static public Calendar parseAmericanDate(final String date) + { + Calendar result; + + Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN); + Matcher matcher = pattern.matcher(date); + + 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))); + } + else + { + result = null; + } + + // + return (result); + } + + /** + * Note: European parsing test made before the American parsing one. + */ + static public Calendar parseDate(final String date) + { + Calendar result; + + if (isEuropeanFormat(date)) + { + result = parseEuropeanDate(date); + } + else if (isRawFormat(date)) + { + result = parseRawDate(date); + } + else if (isISOFormat(date)) + { + result = parseISODate(date); + } + else if (isAmericanFormat(date)) + { + result = parseAmericanDate(date); + } + else + { + result = null; + } + + // + return (result); + } + + /** + * + */ + static public Calendar parseEuropeanDate(final String date) + { + Calendar result; + + Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN); + Matcher matcher = pattern.matcher(date); + + 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))); + } + else + { + result = null; + } + + // + return (result); + } + + /** + * + */ + static public Calendar parseISODate(final String date) + { + Calendar result; + + Pattern pattern = Pattern.compile(ISO_DATE_PATTERN); + Matcher matcher = pattern.matcher(date); + + 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))); + } + else + { + result = null; + } + + // + return (result); + } + + /** + * + */ + static public Calendar parseRawDate(final String date) + { + Calendar result; + + Pattern pattern = Pattern.compile(RAW_DATE_PATTERN); + Matcher matcher = pattern.matcher(date); + + 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))); + } + else + { + result = null; + } + + // + return (result); + } + + /** + * + */ + static public String rawFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1); + } + + // + return (result); + } + + /** + * + */ + static public String shortEuropeanFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1); + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/DateTimeHelper.java b/src/fr/devinsy/util/DateTimeHelper.java index 89be3a4..c43e80e 100755 --- a/src/fr/devinsy/util/DateTimeHelper.java +++ b/src/fr/devinsy/util/DateTimeHelper.java @@ -1,111 +1,111 @@ -/** - * @author Christian Momon, June 2010. - * 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. - */ -package fr.devinsy.util; - -import java.util.Calendar; - -/** - * This class groups function to help in Calendar manipulation. - */ -public class DateTimeHelper -{ - // static private final Logger logger = - // LoggerFactory.getLogger(DateTimeHelper.class); - - 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 ISO_DATE_FORMAT = "%04d-%02d-%02d %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 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 AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; - - /** - * - */ - static public String americanFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - 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), - time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); - } - - // - return (result); - } - - /** - * - */ - static public String europeanFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - 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), - time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); - } - - // - return (result); - } - - /** - * - */ - static public String ISOFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - 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), - time.get(Calendar.SECOND)); - } - - // - return (result); - } - - /** - * - */ - static public String rawFormat(final Calendar time) - { - String result; - - if (time == null) - { - result = ""; - } - 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), - time.get(Calendar.SECOND)); - } - - // - return (result); - } -} +/** + * @author Christian Momon, June 2010. + * 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. + */ +package fr.devinsy.util; + +import java.util.Calendar; + +/** + * This class groups function to help in Calendar manipulation. + */ +public class DateTimeHelper +{ + // static private final Logger logger = + // LoggerFactory.getLogger(DateTimeHelper.class); + + 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 ISO_DATE_FORMAT = "%04d-%02d-%02d %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 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 AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; + + /** + * + */ + static public String americanFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + 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), + time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); + } + + // + return (result); + } + + /** + * + */ + static public String europeanFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + 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), + time.get(Calendar.MINUTE), time.get(Calendar.SECOND)); + } + + // + return (result); + } + + /** + * + */ + static public String ISOFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + 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), + time.get(Calendar.SECOND)); + } + + // + return (result); + } + + /** + * + */ + static public String rawFormat(final Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + 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), + time.get(Calendar.SECOND)); + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/InternetProxyConfiguration.java b/src/fr/devinsy/util/InternetProxyConfiguration.java index 2001866..63134ad 100755 --- a/src/fr/devinsy/util/InternetProxyConfiguration.java +++ b/src/fr/devinsy/util/InternetProxyConfiguration.java @@ -1,228 +1,228 @@ -/** - * @author Christian Momon, September 2009. - * 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. - */ -package fr.devinsy.util; - -/** - * - */ -public class InternetProxyConfiguration -{ - // - protected String host; - protected int port; - protected String login; - protected String password; - - /** - * - */ - public InternetProxyConfiguration() - { - this.host = ""; - this.port = 0; - this.login = ""; - this.password = ""; - } - - /** - * - */ - public InternetProxyConfiguration(final String host, final int port, final String login, final String password) - { - // - if (host == null) - { - this.host = ""; - } - else - { - this.host = host; - } - - // - this.port = port; - - // - if (login == null) - { - this.login = ""; - } - else - { - this.login = login; - } - - // - if (password == null) - { - this.password = ""; - } - else - { - this.password = password; - } - } - - /** - * - */ - public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception - { - // - if (host == null) - { - this.host = ""; - } - else - { - this.host = host; - } - - // - if ((port == null) || (port.trim().length() == 0)) - { - this.port = 0; - } - else - { - try - { - this.port = Integer.parseInt(port); - } - catch (Exception exception) - { - String errorMessage = "Incorrect PROXY port value."; - throw new Exception(errorMessage, exception); - } - } - - // - if (login == null) - { - this.login = ""; - } - else - { - this.login = login; - } - - // - if (password == null) - { - this.password = ""; - } - else - { - this.password = password; - } - } - - /** - * - */ - public String host() - { - String result; - - result = this.host; - - // - return (result); - } - - /** - * - */ - public boolean isInitialized() - { - boolean result; - - if ((this.host.length() > 0) && (this.port > 0)) - { - result = true; - } - else - { - result = false; - } - - // - return (result); - } - - /** - * - */ - public String login() - { - String result; - - result = this.login; - - // - return (result); - } - - /** - * - */ - public String password() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public int port() - { - int result; - - result = this.port; - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - String login; - if (this.login.length() == 0) - { - login = ""; - } - else - { - login = "********"; - } - - String password; - if (this.password.length() == 0) - { - password = ""; - } - else - { - password = "********"; - } - - result = "(" + this.host + "," + this.port + "," + login + "," + password + ")"; - - // - return (result); - } -} +/** + * @author Christian Momon, September 2009. + * 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. + */ +package fr.devinsy.util; + +/** + * + */ +public class InternetProxyConfiguration +{ + // + protected String host; + protected int port; + protected String login; + protected String password; + + /** + * + */ + public InternetProxyConfiguration() + { + this.host = ""; + this.port = 0; + this.login = ""; + this.password = ""; + } + + /** + * + */ + public InternetProxyConfiguration(final String host, final int port, final String login, final String password) + { + // + if (host == null) + { + this.host = ""; + } + else + { + this.host = host; + } + + // + this.port = port; + + // + if (login == null) + { + this.login = ""; + } + else + { + this.login = login; + } + + // + if (password == null) + { + this.password = ""; + } + else + { + this.password = password; + } + } + + /** + * + */ + public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception + { + // + if (host == null) + { + this.host = ""; + } + else + { + this.host = host; + } + + // + if ((port == null) || (port.trim().length() == 0)) + { + this.port = 0; + } + else + { + try + { + this.port = Integer.parseInt(port); + } + catch (Exception exception) + { + String errorMessage = "Incorrect PROXY port value."; + throw new Exception(errorMessage, exception); + } + } + + // + if (login == null) + { + this.login = ""; + } + else + { + this.login = login; + } + + // + if (password == null) + { + this.password = ""; + } + else + { + this.password = password; + } + } + + /** + * + */ + public String host() + { + String result; + + result = this.host; + + // + return (result); + } + + /** + * + */ + public boolean isInitialized() + { + boolean result; + + if ((this.host.length() > 0) && (this.port > 0)) + { + result = true; + } + else + { + result = false; + } + + // + return (result); + } + + /** + * + */ + public String login() + { + String result; + + result = this.login; + + // + return (result); + } + + /** + * + */ + public String password() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public int port() + { + int result; + + result = this.port; + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + String login; + if (this.login.length() == 0) + { + login = ""; + } + else + { + login = "********"; + } + + String password; + if (this.password.length() == 0) + { + password = ""; + } + else + { + password = "********"; + } + + result = "(" + this.host + "," + this.port + "," + login + "," + password + ")"; + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/SimpleAveragemeter.java b/src/fr/devinsy/util/SimpleAveragemeter.java index d49a4a7..7168fae 100755 --- a/src/fr/devinsy/util/SimpleAveragemeter.java +++ b/src/fr/devinsy/util/SimpleAveragemeter.java @@ -1,120 +1,120 @@ -/** - * @author Christian Momon, September 2009. - * 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. - */ -package fr.devinsy.util; - -/** - * 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. - */ -public class SimpleAveragemeter -{ - // - protected long sum; - protected long cardinal; - protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond. - - /** - * - */ - public SimpleAveragemeter() - { - this.reset(); - } - - /** - * - */ - synchronized public void add(final long value) - { - // Manage the sum limit. - if ((this.sum > Long.MAX_VALUE / 2) && (cardinal % 2 == 0)) - { - this.sum = this.sum / 2; - this.cardinal = this.cardinal / 2; - } - - // Add the new value. - if (this.sum > MAX_ADD) - { - this.sum += MAX_ADD; - this.cardinal += 1; - } - else - { - this.sum += value; - this.cardinal += 1; - } - } - - /** - * - */ - synchronized public long average() - { - long result; - - if (cardinal == 0) - { - result = 0; - } - else - { - result = sum / cardinal; - } - - // - return (result); - } - - /** - * - */ - public long cardinal() - { - long result; - - result = this.cardinal; - - // - return (result); - } - - /** - * - */ - synchronized public void reset() - { - this.sum = 0; - this.cardinal = 0; - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = Long.toString(this.average()); - - // - return (result); - } - - /** - * - */ - public long value() - { - long result; - - result = this.average(); - - // - return (result); - } -} +/** + * @author Christian Momon, September 2009. + * 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. + */ +package fr.devinsy.util; + +/** + * 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. + */ +public class SimpleAveragemeter +{ + // + protected long sum; + protected long cardinal; + protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond. + + /** + * + */ + public SimpleAveragemeter() + { + this.reset(); + } + + /** + * + */ + synchronized public void add(final long value) + { + // Manage the sum limit. + if ((this.sum > Long.MAX_VALUE / 2) && (cardinal % 2 == 0)) + { + this.sum = this.sum / 2; + this.cardinal = this.cardinal / 2; + } + + // Add the new value. + if (this.sum > MAX_ADD) + { + this.sum += MAX_ADD; + this.cardinal += 1; + } + else + { + this.sum += value; + this.cardinal += 1; + } + } + + /** + * + */ + synchronized public long average() + { + long result; + + if (cardinal == 0) + { + result = 0; + } + else + { + result = sum / cardinal; + } + + // + return (result); + } + + /** + * + */ + public long cardinal() + { + long result; + + result = this.cardinal; + + // + return (result); + } + + /** + * + */ + synchronized public void reset() + { + this.sum = 0; + this.cardinal = 0; + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = Long.toString(this.average()); + + // + return (result); + } + + /** + * + */ + public long value() + { + long result; + + result = this.average(); + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/SimpleChronometer.java b/src/fr/devinsy/util/SimpleChronometer.java index 637e5a3..ae31976 100755 --- a/src/fr/devinsy/util/SimpleChronometer.java +++ b/src/fr/devinsy/util/SimpleChronometer.java @@ -1,151 +1,151 @@ -/** - * @author Christian Momon, in June 2008. - * 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. - */ -package fr.devinsy.util; - -import java.util.Date; - -/** - * - */ -public class SimpleChronometer -{ - // - protected long firstTime; - - /** - * - */ - public SimpleChronometer() - { - this.reset(); - } - - /** - * - */ - public long interval() - { - long result; - - result = new Date().getTime() - this.firstTime; - - // - return (result); - } - - /** - * - */ - public void reset() - { - this.firstTime = new Date().getTime(); - } - - /** - * TO BE COMPLETED. - */ - static public String humanString(final long interval) - { - String result; - - if (interval < 1000) - { - result = interval + "ms"; - } - else if (interval < 60 * 1000) - { - result = interval / 1000 + "," + interval % 1000 + "s"; - } - else if (interval < 60 * 60 * 1000) - { - result = interval / 1000 + "," + interval % 1000 + "s"; - } - else if (interval < 24 * 60 * 60 * 1000) - { - result = interval / 1000 + "," + interval % 1000 + "s"; - } - else if (interval < 7 * 24 * 60 * 60 * 1000) - { - result = interval / 1000 + "," + interval % 1000 + "s"; - } - else - // if (interval < 7*24*60*60*1000) - { - result = interval / 1000 + "," + interval % 1000 + "s"; - } - - // - return (result); - } - - /** - * - */ - static public String shortHumanString(final long interval) - { - String result; - - if (interval < 1000) - { - result = interval + " ms"; - } - else if (interval < 2 * 1000) - { - result = interval / 1000 + " seconde"; - } - else if (interval < 60 * 1000) - { - result = interval / 1000 + " secondes"; - } - else if (interval < 2 * 60 * 1000L) - { - result = interval / (60 * 1000L) + " minute"; - } - else if (interval < 60 * 60 * 1000L) - { - result = interval / (60 * 1000L) + " minutes"; - } - else if (interval < 2 * 60 * 60 * 1000L) - { - result = interval / (60 * 60 * 1000L) + " heure"; - } - else if (interval < 24 * 60 * 60 * 1000L) - { - result = interval / (60 * 60 * 1000L) + " heures"; - } - else if (interval < 2 * 24 * 60 * 60 * 1000L) - { - result = interval / (24 * 60 * 60 * 1000L) + " jour"; - } - else if (interval < 7 * 24 * 60 * 60 * 1000L) - { - result = interval / (24 * 60 * 60 * 1000L) + " jours"; - } - else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L) - { - result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine"; - } - else if (interval < 30 * 24 * 60 * 60 * 1000L) - { - result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines"; - } - else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L) - { - result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois"; - } - else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L) - { - result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année"; - } - else - { - result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années"; - } - - // - return (result); - } -} +/** + * @author Christian Momon, in June 2008. + * 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. + */ +package fr.devinsy.util; + +import java.util.Date; + +/** + * + */ +public class SimpleChronometer +{ + // + protected long firstTime; + + /** + * + */ + public SimpleChronometer() + { + this.reset(); + } + + /** + * + */ + public long interval() + { + long result; + + result = new Date().getTime() - this.firstTime; + + // + return (result); + } + + /** + * + */ + public void reset() + { + this.firstTime = new Date().getTime(); + } + + /** + * TO BE COMPLETED. + */ + static public String humanString(final long interval) + { + String result; + + if (interval < 1000) + { + result = interval + "ms"; + } + else if (interval < 60 * 1000) + { + result = interval / 1000 + "," + interval % 1000 + "s"; + } + else if (interval < 60 * 60 * 1000) + { + result = interval / 1000 + "," + interval % 1000 + "s"; + } + else if (interval < 24 * 60 * 60 * 1000) + { + result = interval / 1000 + "," + interval % 1000 + "s"; + } + else if (interval < 7 * 24 * 60 * 60 * 1000) + { + result = interval / 1000 + "," + interval % 1000 + "s"; + } + else + // if (interval < 7*24*60*60*1000) + { + result = interval / 1000 + "," + interval % 1000 + "s"; + } + + // + return (result); + } + + /** + * + */ + static public String shortHumanString(final long interval) + { + String result; + + if (interval < 1000) + { + result = interval + " ms"; + } + else if (interval < 2 * 1000) + { + result = interval / 1000 + " seconde"; + } + else if (interval < 60 * 1000) + { + result = interval / 1000 + " secondes"; + } + else if (interval < 2 * 60 * 1000L) + { + result = interval / (60 * 1000L) + " minute"; + } + else if (interval < 60 * 60 * 1000L) + { + result = interval / (60 * 1000L) + " minutes"; + } + else if (interval < 2 * 60 * 60 * 1000L) + { + result = interval / (60 * 60 * 1000L) + " heure"; + } + else if (interval < 24 * 60 * 60 * 1000L) + { + result = interval / (60 * 60 * 1000L) + " heures"; + } + else if (interval < 2 * 24 * 60 * 60 * 1000L) + { + result = interval / (24 * 60 * 60 * 1000L) + " jour"; + } + else if (interval < 7 * 24 * 60 * 60 * 1000L) + { + result = interval / (24 * 60 * 60 * 1000L) + " jours"; + } + else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L) + { + result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine"; + } + else if (interval < 30 * 24 * 60 * 60 * 1000L) + { + result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines"; + } + else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L) + { + result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois"; + } + else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L) + { + result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année"; + } + else + { + result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années"; + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/StringConcatenator.java b/src/fr/devinsy/util/StringConcatenator.java index 9dddfe5..8a9b895 100755 --- a/src/fr/devinsy/util/StringConcatenator.java +++ b/src/fr/devinsy/util/StringConcatenator.java @@ -1,383 +1,383 @@ -/** - * @author Christian Momon, June 2008. - * 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. - */ -package fr.devinsy.util; - -import java.io.IOException; -import java.util.ArrayList; - -/** - * 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 - * building of strings where they are lot of concatenation action. - */ -public class StringConcatenator extends ArrayList -{ - private static final long serialVersionUID = -1154185934830213732L; - public String LINE_SEPARATOR = "\n"; - - /** - * - */ - public StringConcatenator() - { - super(); - } - - /** - * - */ - public StringConcatenator append(final char character) - { - StringConcatenator result; - - this.add(String.valueOf(character)); - - result = this; - - // - return (result); - } - - /** - * - */ - public StringConcatenator append(final String string) - { - StringConcatenator result; - - if (string != null) - { - this.add(string); - } - - result = this; - - // - return (result); - } - - /** - * - */ - public StringConcatenator append(final StringConcatenator string) - { - StringConcatenator result; - - if (string != null) - { - for (int nString = 0; nString < string.size(); nString++) - { - this.add(string.getByIndex(nString)); - } - } - - result = this; - - // - return (result); - } - - /** - * - */ - public StringConcatenator appendln() - { - StringConcatenator result; - - this.add(LINE_SEPARATOR); - - result = this; - - // - return (result); - } - - /** - * - */ - public StringConcatenator appendln(final char character) - { - StringConcatenator result; - - result = this.append(character).appendln(); - - // - return (result); - } - - /** - * - */ - public StringConcatenator appendln(final String string) - { - StringConcatenator result; - - result = this.append(string).appendln(); - - // - return (result); - } - - /** - * - */ - public StringConcatenator appendln(final StringConcatenator string) - { - StringConcatenator result; - - result = this.append(string).appendln(); - - // - return (result); - } - - /** - * - */ - public String getByIndex(final int id) - { - String result; - - result = this.get(id); - - // - return (result); - } - - /** - * - */ - public int lenght() - { - int result = 0; - - for (int nString = 0; nString < this.size(); nString++) - { - result += this.getByIndex(nString).length(); - - } - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - StringBuffer preResult = new StringBuffer(this.lenght()); - - for (int nString = 0; nString < this.size(); nString++) - { - preResult.append(this.getByIndex(nString)); - } - - result = new String(preResult); - - // - return (result); - } - - /** - * - */ - public void writeInto(final java.io.Writer out) throws IOException - { - for (int nString = 0; nString < this.size(); nString++) - { - out.write(this.getByIndex(nString)); - } - } - - /** - * - */ - static public String toString(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator string = new StringConcatenator(); - - for (int nString = 0; nString < strings.length; nString++) - { - string.append(strings[nString]); - - if (nString < strings.length - 1) - { - string.append(' '); - } - } - - result = string.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringNotNull(final String[] strings) - { - String result; - - result = toString(strings); - - if (result == null) - { - result = ""; - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBracket(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - merge.append("["); - merge.append(toStringWithCommas(strings)); - merge.append("]"); - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBracketNotNull(final String[] strings) - { - String result; - - result = toStringWithBrackets(strings); - - if (result == null) - { - result = ""; - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBrackets(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - merge.append("[").append(string).append("]"); - } - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithCommas(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - if (merge.size() != 0) - { - merge.append(","); - } - - merge.append(string); - } - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithFrenchCommas(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - if (merge.size() != 0) - { - merge.append(", "); - } - - merge.append(string); - } - - result = merge.toString(); - } - - // - return (result); - } -} +/** + * @author Christian Momon, June 2008. + * 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. + */ +package fr.devinsy.util; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * 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 + * building of strings where they are lot of concatenation action. + */ +public class StringConcatenator extends ArrayList +{ + private static final long serialVersionUID = -1154185934830213732L; + public String LINE_SEPARATOR = "\n"; + + /** + * + */ + public StringConcatenator() + { + super(); + } + + /** + * + */ + public StringConcatenator append(final char character) + { + StringConcatenator result; + + this.add(String.valueOf(character)); + + result = this; + + // + return (result); + } + + /** + * + */ + public StringConcatenator append(final String string) + { + StringConcatenator result; + + if (string != null) + { + this.add(string); + } + + result = this; + + // + return (result); + } + + /** + * + */ + public StringConcatenator append(final StringConcatenator string) + { + StringConcatenator result; + + if (string != null) + { + for (int nString = 0; nString < string.size(); nString++) + { + this.add(string.getByIndex(nString)); + } + } + + result = this; + + // + return (result); + } + + /** + * + */ + public StringConcatenator appendln() + { + StringConcatenator result; + + this.add(LINE_SEPARATOR); + + result = this; + + // + return (result); + } + + /** + * + */ + public StringConcatenator appendln(final char character) + { + StringConcatenator result; + + result = this.append(character).appendln(); + + // + return (result); + } + + /** + * + */ + public StringConcatenator appendln(final String string) + { + StringConcatenator result; + + result = this.append(string).appendln(); + + // + return (result); + } + + /** + * + */ + public StringConcatenator appendln(final StringConcatenator string) + { + StringConcatenator result; + + result = this.append(string).appendln(); + + // + return (result); + } + + /** + * + */ + public String getByIndex(final int id) + { + String result; + + result = this.get(id); + + // + return (result); + } + + /** + * + */ + public int lenght() + { + int result = 0; + + for (int nString = 0; nString < this.size(); nString++) + { + result += this.getByIndex(nString).length(); + + } + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer preResult = new StringBuffer(this.lenght()); + + for (int nString = 0; nString < this.size(); nString++) + { + preResult.append(this.getByIndex(nString)); + } + + result = new String(preResult); + + // + return (result); + } + + /** + * + */ + public void writeInto(final java.io.Writer out) throws IOException + { + for (int nString = 0; nString < this.size(); nString++) + { + out.write(this.getByIndex(nString)); + } + } + + /** + * + */ + static public String toString(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator string = new StringConcatenator(); + + for (int nString = 0; nString < strings.length; nString++) + { + string.append(strings[nString]); + + if (nString < strings.length - 1) + { + string.append(' '); + } + } + + result = string.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringNotNull(final String[] strings) + { + String result; + + result = toString(strings); + + if (result == null) + { + result = ""; + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBracket(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + merge.append("["); + merge.append(toStringWithCommas(strings)); + merge.append("]"); + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBracketNotNull(final String[] strings) + { + String result; + + result = toStringWithBrackets(strings); + + if (result == null) + { + result = ""; + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBrackets(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + merge.append("[").append(string).append("]"); + } + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithCommas(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + if (merge.size() != 0) + { + merge.append(","); + } + + merge.append(string); + } + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithFrenchCommas(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + if (merge.size() != 0) + { + merge.append(", "); + } + + merge.append(string); + } + + result = merge.toString(); + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/StringList.java b/src/fr/devinsy/util/StringList.java index ba55d21..fb26648 100755 --- a/src/fr/devinsy/util/StringList.java +++ b/src/fr/devinsy/util/StringList.java @@ -1,514 +1,514 @@ -/** - * @author Christian Momon, June 2008. - * 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. - */ -package fr.devinsy.util; - -import java.io.IOException; -import java.util.ArrayList; - -/** - * 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 - * building of strings where they are lot of concatenation action. - */ -public class StringList extends ArrayList -{ - private static final long serialVersionUID = -1154185934830213732L; - public String LINE_SEPARATOR = "\n"; - - /** - * - */ - public StringList() - { - super(); - } - - /** - * - */ - public StringList(final int size) - { - super(size); - } - - /** - * - */ - public StringList append(final char character) - { - StringList result; - - this.add(String.valueOf(character)); - - result = this; - - // - return (result); - } - - /** - * - */ - public StringList append(final int value) - { - StringList result; - - result = this.append(String.valueOf(value)); - - // - return (result); - } - - /** - * - */ - public StringList append(final long value) - { - StringList result; - - result = this.append(String.valueOf(value)); - - // - return (result); - } - - /** - * Check null parameter before add. - */ - public StringList append(final String string) - { - StringList result; - - if (string != null) - { - this.add(string); - } - - result = this; - - // - return (result); - } - - /** - * - */ - public StringList append(final String... strings) - { - StringList result; - - if (strings != null) - { - for (String string : strings) - { - this.add(string); - } - } - - result = this; - - // - return (result); - } - - /** - * - */ - public StringList append(final StringList string) - { - StringList result; - - if (string != null) - { - for (int nString = 0; nString < string.size(); nString++) - { - this.add(string.getByIndex(nString)); - } - } - - result = this; - - // - return (result); - } - - /** - * - */ - public StringList appendln() - { - StringList result; - - this.add(LINE_SEPARATOR); - - result = this; - - // - return (result); - } - - /** - * - */ - public StringList appendln(final char character) - { - StringList result; - - result = this.append(character).appendln(); - - // - return (result); - } - - /** - * - */ - public StringList appendln(final int value) - { - StringList result; - - result = this.append(value).appendln(); - - // - return (result); - } - - /** - * - */ - public StringList appendln(final long value) - { - StringList result; - - result = this.append(value).appendln(); - - // - return (result); - } - - /** - * - */ - public StringList appendln(final String string) - { - StringList result; - - result = this.append(string).appendln(); - - // - return (result); - } - - /** - * - */ - public StringList appendln(final String... strings) - { - StringList result; - - result = this.append(strings).appendln(); - - // - return (result); - } - - /** - * - */ - public StringList appendln(final StringList string) - { - StringList result; - - result = this.append(string).appendln(); - - // - return (result); - } - - /** - * - */ - public String getByIndex(final int id) - { - String result; - - result = this.get(id); - - // - return (result); - } - - /** - * - */ - public int lenght() - { - int result = 0; - - for (int nString = 0; nString < this.size(); nString++) - { - result += this.getByIndex(nString).length(); - - } - - // - return (result); - } - - /** - * - */ - public StringList removeLast() - { - StringList result; - - if (this.size() > 0) - { - this.remove(this.size() - 1); - } - - result = this; - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - StringBuffer preResult = new StringBuffer(this.lenght()); - - for (int nString = 0; nString < this.size(); nString++) - { - preResult.append(this.getByIndex(nString)); - } - - result = new String(preResult); - - // - return (result); - } - - /** - * - */ - public void writeInto(final java.io.Writer out) throws IOException - { - for (int nString = 0; nString < this.size(); nString++) - { - out.write(this.getByIndex(nString)); - } - } - - /** - * - */ - static public String multiply(final String source, final int number) - { - String result; - - StringList strings = new StringList(); - for (int index = 0; index < number; index++) - { - strings.append(source); - } - - result = strings.toString(); - - // - return (result); - } - - /** - * - */ - static public String toString(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator string = new StringConcatenator(); - - for (int nString = 0; nString < strings.length; nString++) - { - string.append(strings[nString]); - - if (nString < strings.length - 1) - { - string.append(' '); - } - } - - result = string.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringNotNull(final String[] strings) - { - String result; - - result = toString(strings); - - if (result == null) - { - result = ""; - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBracket(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - merge.append("["); - merge.append(toStringWithCommas(strings)); - merge.append("]"); - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBracketNotNull(final String[] strings) - { - String result; - - result = toStringWithBrackets(strings); - - if (result == null) - { - result = ""; - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithBrackets(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - merge.append("[").append(string).append("]"); - } - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithCommas(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - if (merge.size() != 0) - { - merge.append(","); - } - - merge.append(string); - } - - result = merge.toString(); - } - - // - return (result); - } - - /** - * - */ - static public String toStringWithFrenchCommas(final String[] strings) - { - String result; - - if (strings == null) - { - result = null; - } - else - { - StringConcatenator merge = new StringConcatenator(); - - for (String string : strings) - { - if (merge.size() != 0) - { - merge.append(", "); - } - - merge.append(string); - } - - result = merge.toString(); - } - - // - return (result); - } -} +/** + * @author Christian Momon, June 2008. + * 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. + */ +package fr.devinsy.util; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * 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 + * building of strings where they are lot of concatenation action. + */ +public class StringList extends ArrayList +{ + private static final long serialVersionUID = -1154185934830213732L; + public String LINE_SEPARATOR = "\n"; + + /** + * + */ + public StringList() + { + super(); + } + + /** + * + */ + public StringList(final int size) + { + super(size); + } + + /** + * + */ + public StringList append(final char character) + { + StringList result; + + this.add(String.valueOf(character)); + + result = this; + + // + return (result); + } + + /** + * + */ + public StringList append(final int value) + { + StringList result; + + result = this.append(String.valueOf(value)); + + // + return (result); + } + + /** + * + */ + public StringList append(final long value) + { + StringList result; + + result = this.append(String.valueOf(value)); + + // + return (result); + } + + /** + * Check null parameter before add. + */ + public StringList append(final String string) + { + StringList result; + + if (string != null) + { + this.add(string); + } + + result = this; + + // + return (result); + } + + /** + * + */ + public StringList append(final String... strings) + { + StringList result; + + if (strings != null) + { + for (String string : strings) + { + this.add(string); + } + } + + result = this; + + // + return (result); + } + + /** + * + */ + public StringList append(final StringList string) + { + StringList result; + + if (string != null) + { + for (int nString = 0; nString < string.size(); nString++) + { + this.add(string.getByIndex(nString)); + } + } + + result = this; + + // + return (result); + } + + /** + * + */ + public StringList appendln() + { + StringList result; + + this.add(LINE_SEPARATOR); + + result = this; + + // + return (result); + } + + /** + * + */ + public StringList appendln(final char character) + { + StringList result; + + result = this.append(character).appendln(); + + // + return (result); + } + + /** + * + */ + public StringList appendln(final int value) + { + StringList result; + + result = this.append(value).appendln(); + + // + return (result); + } + + /** + * + */ + public StringList appendln(final long value) + { + StringList result; + + result = this.append(value).appendln(); + + // + return (result); + } + + /** + * + */ + public StringList appendln(final String string) + { + StringList result; + + result = this.append(string).appendln(); + + // + return (result); + } + + /** + * + */ + public StringList appendln(final String... strings) + { + StringList result; + + result = this.append(strings).appendln(); + + // + return (result); + } + + /** + * + */ + public StringList appendln(final StringList string) + { + StringList result; + + result = this.append(string).appendln(); + + // + return (result); + } + + /** + * + */ + public String getByIndex(final int id) + { + String result; + + result = this.get(id); + + // + return (result); + } + + /** + * + */ + public int lenght() + { + int result = 0; + + for (int nString = 0; nString < this.size(); nString++) + { + result += this.getByIndex(nString).length(); + + } + + // + return (result); + } + + /** + * + */ + public StringList removeLast() + { + StringList result; + + if (this.size() > 0) + { + this.remove(this.size() - 1); + } + + result = this; + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer preResult = new StringBuffer(this.lenght()); + + for (int nString = 0; nString < this.size(); nString++) + { + preResult.append(this.getByIndex(nString)); + } + + result = new String(preResult); + + // + return (result); + } + + /** + * + */ + public void writeInto(final java.io.Writer out) throws IOException + { + for (int nString = 0; nString < this.size(); nString++) + { + out.write(this.getByIndex(nString)); + } + } + + /** + * + */ + static public String multiply(final String source, final int number) + { + String result; + + StringList strings = new StringList(); + for (int index = 0; index < number; index++) + { + strings.append(source); + } + + result = strings.toString(); + + // + return (result); + } + + /** + * + */ + static public String toString(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator string = new StringConcatenator(); + + for (int nString = 0; nString < strings.length; nString++) + { + string.append(strings[nString]); + + if (nString < strings.length - 1) + { + string.append(' '); + } + } + + result = string.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringNotNull(final String[] strings) + { + String result; + + result = toString(strings); + + if (result == null) + { + result = ""; + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBracket(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + merge.append("["); + merge.append(toStringWithCommas(strings)); + merge.append("]"); + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBracketNotNull(final String[] strings) + { + String result; + + result = toStringWithBrackets(strings); + + if (result == null) + { + result = ""; + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithBrackets(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + merge.append("[").append(string).append("]"); + } + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithCommas(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + if (merge.size() != 0) + { + merge.append(","); + } + + merge.append(string); + } + + result = merge.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String toStringWithFrenchCommas(final String[] strings) + { + String result; + + if (strings == null) + { + result = null; + } + else + { + StringConcatenator merge = new StringConcatenator(); + + for (String string : strings) + { + if (merge.size() != 0) + { + merge.append(", "); + } + + merge.append(string); + } + + result = merge.toString(); + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/util/StringListWriter.java b/src/fr/devinsy/util/StringListWriter.java index c903154..378dbe7 100755 --- a/src/fr/devinsy/util/StringListWriter.java +++ b/src/fr/devinsy/util/StringListWriter.java @@ -1,77 +1,77 @@ -/** - * @author Christian Momon, March 2010. - * 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. - */ -package fr.devinsy.util; - -import java.io.IOException; -import java.io.Writer; - -/** - * - */ -public class StringListWriter extends Writer -{ - protected StringList out; - - /** - * - */ - StringListWriter() - { - this.out = new StringList(); - } - - /** - * - */ - StringListWriter(final int size) - { - this.out = new StringList(size); - } - - /* - * - */ - @Override - public void close() throws IOException - { - - } - - /* - * - */ - @Override - public void flush() throws IOException - { - - } - - /* - * - */ - public void write(final char c) throws IOException - { - this.out.append(c); - } - - /* - * - */ - @Override - public void write(final char[] cbuf, final int off, final int len) throws IOException - { - this.out.append(cbuf.toString().substring(off, len)); - } - - /* - * - */ - @Override - public void write(final String string) throws IOException - { - this.out.append(string); - } -} +/** + * @author Christian Momon, March 2010. + * 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. + */ +package fr.devinsy.util; + +import java.io.IOException; +import java.io.Writer; + +/** + * + */ +public class StringListWriter extends Writer +{ + protected StringList out; + + /** + * + */ + StringListWriter() + { + this.out = new StringList(); + } + + /** + * + */ + StringListWriter(final int size) + { + this.out = new StringList(size); + } + + /* + * + */ + @Override + public void close() throws IOException + { + + } + + /* + * + */ + @Override + public void flush() throws IOException + { + + } + + /* + * + */ + public void write(final char c) throws IOException + { + this.out.append(c); + } + + /* + * + */ + @Override + public void write(final char[] cbuf, final int off, final int len) throws IOException + { + this.out.append(cbuf.toString().substring(off, len)); + } + + /* + * + */ + @Override + public void write(final String string) throws IOException + { + this.out.append(string); + } +} diff --git a/src/fr/devinsy/util/cmdexec/Wrapper.java b/src/fr/devinsy/util/cmdexec/Wrapper.java index 5acb43d..f672c3c 100644 --- a/src/fr/devinsy/util/cmdexec/Wrapper.java +++ b/src/fr/devinsy/util/cmdexec/Wrapper.java @@ -1,67 +1,67 @@ -package fr.devinsy.util.cmdexec; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintWriter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - */ -public class Wrapper -{ - static private final Wrapper instance = new Wrapper(); - static private final Logger logger = LoggerFactory.getLogger(Wrapper.class); - - /** - * return instance of the controller - */ - public static Wrapper instance() - { - return (instance); - } - - /** - * - */ - static public StringBuffer wrap(final String ressource) throws IOException - { - logger.info("Enter"); - StringBuffer result; - - result = new StringBuffer(); - - BufferedReader buf = new BufferedReader(new FileReader(ressource)); - String ligne; - while ((ligne = buf.readLine()) != null) - { - result.append(ligne + "\n"); - } - - // - logger.info("Exit"); - return (result); - } - - /** - * - */ - static public void wrap(final String ressource, final PrintWriter output) throws IOException - { - logger.info("Enter"); - - BufferedReader buf = new BufferedReader(new FileReader(ressource)); - String ligne; - while ((ligne = buf.readLine()) != null) - { - output.print(ligne); - } - - logger.info("Exit"); - } -} - +package fr.devinsy.util.cmdexec; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class Wrapper +{ + static private final Wrapper instance = new Wrapper(); + static private final Logger logger = LoggerFactory.getLogger(Wrapper.class); + + /** + * return instance of the controller + */ + public static Wrapper instance() + { + return (instance); + } + + /** + * + */ + static public StringBuffer wrap(final String ressource) throws IOException + { + logger.info("Enter"); + StringBuffer result; + + result = new StringBuffer(); + + BufferedReader buf = new BufferedReader(new FileReader(ressource)); + String ligne; + while ((ligne = buf.readLine()) != null) + { + result.append(ligne + "\n"); + } + + // + logger.info("Exit"); + return (result); + } + + /** + * + */ + static public void wrap(final String ressource, final PrintWriter output) throws IOException + { + logger.info("Enter"); + + BufferedReader buf = new BufferedReader(new FileReader(ressource)); + String ligne; + while ((ligne = buf.readLine()) != null) + { + output.print(ligne); + } + + logger.info("Exit"); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/CachedFile.java b/src/fr/devinsy/util/unix/CachedFile.java index 906f7c7..6b331b6 100644 --- a/src/fr/devinsy/util/unix/CachedFile.java +++ b/src/fr/devinsy/util/unix/CachedFile.java @@ -1,115 +1,115 @@ -package fr.devinsy.util.unix; - -import java.io.File; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - */ -public class CachedFile -{ - public enum Status - { - NOT_LOAD, EXPIRED, UPDATED - } - - static private final Logger logger = LoggerFactory.getLogger(CachedFile.class); - - protected String sourceName; - protected long sourceTime; - protected Status status; - - /** - * - */ - public CachedFile(final String fileName) - { - this.sourceName = fileName; - this.sourceTime = 0; - this.status = Status.NOT_LOAD; - } - - /** - * - */ - protected File getSourceFile() - { - File result; - - if (this.sourceName == null) - { - result = null; - } - else - { - File source = new File(this.sourceName); - - if (!source.exists()) - { - logger.error("source file defined but not found"); - result = null; - } - else - { - result = source; - } - } - - // - return (result); - } - - /** - * - */ - protected Status getStatus() - { - Status result; - - File source = getSourceFile(); - - if (source == null) - { - this.status = Status.NOT_LOAD; - } - else if (this.sourceTime != source.lastModified()) - { - this.status = Status.EXPIRED; - } - - result = this.status; - - // - return (result); - } - - /** - * - */ - public void setNotLoad() - { - this.status = Status.NOT_LOAD; - } - - /** - * - */ - public void setUpdated() - { - File source = getSourceFile(); - - if (source == null) - { - this.status = Status.NOT_LOAD; - } - else - { - this.sourceTime = source.lastModified(); - this.status = Status.UPDATED; - } - } -} - +package fr.devinsy.util.unix; + +import java.io.File; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class CachedFile +{ + public enum Status + { + NOT_LOAD, EXPIRED, UPDATED + } + + static private final Logger logger = LoggerFactory.getLogger(CachedFile.class); + + protected String sourceName; + protected long sourceTime; + protected Status status; + + /** + * + */ + public CachedFile(final String fileName) + { + this.sourceName = fileName; + this.sourceTime = 0; + this.status = Status.NOT_LOAD; + } + + /** + * + */ + protected File getSourceFile() + { + File result; + + if (this.sourceName == null) + { + result = null; + } + else + { + File source = new File(this.sourceName); + + if (!source.exists()) + { + logger.error("source file defined but not found"); + result = null; + } + else + { + result = source; + } + } + + // + return (result); + } + + /** + * + */ + protected Status getStatus() + { + Status result; + + File source = getSourceFile(); + + if (source == null) + { + this.status = Status.NOT_LOAD; + } + else if (this.sourceTime != source.lastModified()) + { + this.status = Status.EXPIRED; + } + + result = this.status; + + // + return (result); + } + + /** + * + */ + public void setNotLoad() + { + this.status = Status.NOT_LOAD; + } + + /** + * + */ + public void setUpdated() + { + File source = getSourceFile(); + + if (source == null) + { + this.status = Status.NOT_LOAD; + } + else + { + this.sourceTime = source.lastModified(); + this.status = Status.UPDATED; + } + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/EtcGroupFile.java b/src/fr/devinsy/util/unix/EtcGroupFile.java index 2f76c6b..54d2c74 100644 --- a/src/fr/devinsy/util/unix/EtcGroupFile.java +++ b/src/fr/devinsy/util/unix/EtcGroupFile.java @@ -1,181 +1,181 @@ -package fr.devinsy.util.unix; - -import java.util.Vector; - -/** - * - */ -public class EtcGroupFile extends CachedFile -{ - // static private final Logger logger = - // LoggerFactory.getLogger(EtcGroupFile.class); - - static EtcGroupFile instance = null; - protected Groups groups; - - /** - * - */ - protected EtcGroupFile() - { - super("/etc/group"); - this.groups = null; - } - - /* - * - */ - public boolean contains(final String name) - { - boolean result; - - Groups groups = updatedGroups(); - - if (groups == null) - { - result = false; - } - else - { - result = groups.contains(name); - } - - // - return (result); - } - - /** - * - */ - public Group get(final int gid) - { - Group result; - - Groups groups = updatedGroups(); - - if (groups == null) - { - result = null; - } - else - { - result = groups.getByGid(gid); - } - - // - return (result); - } - - /** - * - */ - public Group get(final String name) - { - Group result; - - Groups groups = updatedGroups(); - - if (groups == null) - { - result = null; - } - else - { - result = groups.getByName(name); - } - - // - return (result); - } - - /** - * - */ - public Vector getLoginGroups(final String login) - { - Vector result; - - Groups groups = updatedGroups(); - - result = groups.getLoginGroups(login); - - // - return (result); - } - - /** - * - */ - public String getLoginGroupsString(final String login) - { - String result; - - groups = updatedGroups(); - - result = groups.getLoginGroupsString(login); - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = this.groups.toString(); - - // - return (result); - } - - /** - * - */ - protected Groups updatedGroups() - { - Groups result; - - if (getStatus() != Status.UPDATED) - { - this.groups = EtcGroupFileReader.load(); - - if (this.groups == null) - { - setNotLoad(); - } - else - { - setUpdated(); - } - } - - result = this.groups; - - // - return (result); - } - - /** - * - */ - static public EtcGroupFile instance() - { - EtcGroupFile result; - - if (EtcGroupFile.instance == null) - { - EtcGroupFile.instance = new EtcGroupFile(); - } - - result = EtcGroupFile.instance; - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.util.Vector; + +/** + * + */ +public class EtcGroupFile extends CachedFile +{ + // static private final Logger logger = + // LoggerFactory.getLogger(EtcGroupFile.class); + + static EtcGroupFile instance = null; + protected Groups groups; + + /** + * + */ + protected EtcGroupFile() + { + super("/etc/group"); + this.groups = null; + } + + /* + * + */ + public boolean contains(final String name) + { + boolean result; + + Groups groups = updatedGroups(); + + if (groups == null) + { + result = false; + } + else + { + result = groups.contains(name); + } + + // + return (result); + } + + /** + * + */ + public Group get(final int gid) + { + Group result; + + Groups groups = updatedGroups(); + + if (groups == null) + { + result = null; + } + else + { + result = groups.getByGid(gid); + } + + // + return (result); + } + + /** + * + */ + public Group get(final String name) + { + Group result; + + Groups groups = updatedGroups(); + + if (groups == null) + { + result = null; + } + else + { + result = groups.getByName(name); + } + + // + return (result); + } + + /** + * + */ + public Vector getLoginGroups(final String login) + { + Vector result; + + Groups groups = updatedGroups(); + + result = groups.getLoginGroups(login); + + // + return (result); + } + + /** + * + */ + public String getLoginGroupsString(final String login) + { + String result; + + groups = updatedGroups(); + + result = groups.getLoginGroupsString(login); + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = this.groups.toString(); + + // + return (result); + } + + /** + * + */ + protected Groups updatedGroups() + { + Groups result; + + if (getStatus() != Status.UPDATED) + { + this.groups = EtcGroupFileReader.load(); + + if (this.groups == null) + { + setNotLoad(); + } + else + { + setUpdated(); + } + } + + result = this.groups; + + // + return (result); + } + + /** + * + */ + static public EtcGroupFile instance() + { + EtcGroupFile result; + + if (EtcGroupFile.instance == null) + { + EtcGroupFile.instance = new EtcGroupFile(); + } + + result = EtcGroupFile.instance; + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/EtcGroupFileReader.java b/src/fr/devinsy/util/unix/EtcGroupFileReader.java index 01d47d5..2d7b5f6 100644 --- a/src/fr/devinsy/util/unix/EtcGroupFileReader.java +++ b/src/fr/devinsy/util/unix/EtcGroupFileReader.java @@ -1,83 +1,83 @@ -package fr.devinsy.util.unix; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - */ -public class EtcGroupFileReader -{ - static private final Logger logger = LoggerFactory.getLogger(EtcGroupFileReader.class); - - /** - * - */ - static public Groups load() - { - Groups result; - - BufferedReader file; - - try - { - file = new BufferedReader(new FileReader("/etc/group")); - } - catch (FileNotFoundException exception) - { - logger.error("File not found"); - file = null; - } - - if (file == null) - { - result = null; - } - else - { - result = new Groups(); - - try - { - String line; - while ((line = file.readLine()) != null) - { - String[] tokens = line.split(":"); - - Group group = new Group(); - group.setName(tokens[0]); - group.setPassword(tokens[1]); - group.setGid((new Integer(tokens[2])).intValue()); - - // Manage the case of empty shell. - if (tokens.length == 4) - { - String[] tokensBis = tokens[3].split(","); - - for (int tokenCounter = 0; tokenCounter < tokensBis.length; tokenCounter++) - { - group.addMember(tokensBis[tokenCounter]); - } - } - result.add(group); - } - - file.close(); - } - catch (java.io.IOException exception) - { - logger.error("Exception here."); - result = null; - } - } - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class EtcGroupFileReader +{ + static private final Logger logger = LoggerFactory.getLogger(EtcGroupFileReader.class); + + /** + * + */ + static public Groups load() + { + Groups result; + + BufferedReader file; + + try + { + file = new BufferedReader(new FileReader("/etc/group")); + } + catch (FileNotFoundException exception) + { + logger.error("File not found"); + file = null; + } + + if (file == null) + { + result = null; + } + else + { + result = new Groups(); + + try + { + String line; + while ((line = file.readLine()) != null) + { + String[] tokens = line.split(":"); + + Group group = new Group(); + group.setName(tokens[0]); + group.setPassword(tokens[1]); + group.setGid((new Integer(tokens[2])).intValue()); + + // Manage the case of empty shell. + if (tokens.length == 4) + { + String[] tokensBis = tokens[3].split(","); + + for (int tokenCounter = 0; tokenCounter < tokensBis.length; tokenCounter++) + { + group.addMember(tokensBis[tokenCounter]); + } + } + result.add(group); + } + + file.close(); + } + catch (java.io.IOException exception) + { + logger.error("Exception here."); + result = null; + } + } + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/EtcPasswdFile.java b/src/fr/devinsy/util/unix/EtcPasswdFile.java index 6f73b39..6c2e49d 100644 --- a/src/fr/devinsy/util/unix/EtcPasswdFile.java +++ b/src/fr/devinsy/util/unix/EtcPasswdFile.java @@ -1,167 +1,167 @@ -package fr.devinsy.util.unix; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - */ -public class EtcPasswdFile extends CachedFile -{ - static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFile.class); - - static EtcPasswdFile instance = null; - protected Users users; - - /** - * - */ - protected EtcPasswdFile() - { - super("/etc/passwd"); - this.users = null; - } - - /* - * - */ - public boolean contains(final String login) - { - boolean result; - - Users users = updatedUsers(); - - if (users == null) - { - result = false; - } - else - { - result = users.contains(login); - } - - // - return (result); - } - - /** - * - */ - public User get(final int uid) - { - User result; - - Users users = updatedUsers(); - - if (users == null) - { - result = null; - } - else - { - result = users.getByUid(uid); - } - - // - return (result); - } - - /** - * - */ - public User get(final String login) - { - User result; - - Users users = updatedUsers(); - - if (users == null) - { - result = null; - } - else - { - result = users.getByLogin(login); - } - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = this.users.toString(); - - // - return (result); - } - - /** - * - */ - protected Users update() - { - Users result; - - logger.debug("updating"); - this.users = EtcPasswdFileReader.load(); - - if (this.users == null) - { - setNotLoad(); - } - else - { - setUpdated(); - } - - result = this.users; - - // - return (result); - } - - /** - * - */ - protected Users updatedUsers() - { - Users result; - - if (getStatus() != Status.UPDATED) - { - update(); - } - - result = this.users; - - // - return (result); - } - - /** - * - */ - static public EtcPasswdFile instance() - { - EtcPasswdFile result; - - if (EtcPasswdFile.instance == null) - { - EtcPasswdFile.instance = new EtcPasswdFile(); - } - - result = EtcPasswdFile.instance; - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class EtcPasswdFile extends CachedFile +{ + static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFile.class); + + static EtcPasswdFile instance = null; + protected Users users; + + /** + * + */ + protected EtcPasswdFile() + { + super("/etc/passwd"); + this.users = null; + } + + /* + * + */ + public boolean contains(final String login) + { + boolean result; + + Users users = updatedUsers(); + + if (users == null) + { + result = false; + } + else + { + result = users.contains(login); + } + + // + return (result); + } + + /** + * + */ + public User get(final int uid) + { + User result; + + Users users = updatedUsers(); + + if (users == null) + { + result = null; + } + else + { + result = users.getByUid(uid); + } + + // + return (result); + } + + /** + * + */ + public User get(final String login) + { + User result; + + Users users = updatedUsers(); + + if (users == null) + { + result = null; + } + else + { + result = users.getByLogin(login); + } + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = this.users.toString(); + + // + return (result); + } + + /** + * + */ + protected Users update() + { + Users result; + + logger.debug("updating"); + this.users = EtcPasswdFileReader.load(); + + if (this.users == null) + { + setNotLoad(); + } + else + { + setUpdated(); + } + + result = this.users; + + // + return (result); + } + + /** + * + */ + protected Users updatedUsers() + { + Users result; + + if (getStatus() != Status.UPDATED) + { + update(); + } + + result = this.users; + + // + return (result); + } + + /** + * + */ + static public EtcPasswdFile instance() + { + EtcPasswdFile result; + + if (EtcPasswdFile.instance == null) + { + EtcPasswdFile.instance = new EtcPasswdFile(); + } + + result = EtcPasswdFile.instance; + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/EtcPasswdFileReader.java b/src/fr/devinsy/util/unix/EtcPasswdFileReader.java index 76cbe71..7ef43e1 100644 --- a/src/fr/devinsy/util/unix/EtcPasswdFileReader.java +++ b/src/fr/devinsy/util/unix/EtcPasswdFileReader.java @@ -1,86 +1,86 @@ -package fr.devinsy.util.unix; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - */ -public class EtcPasswdFileReader -{ - static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFileReader.class); - - /** - * - */ - static public Users load() - { - Users result; - - BufferedReader file; - - try - { - file = new BufferedReader(new FileReader("/etc/passwd")); - } - catch (FileNotFoundException exception) - { - logger.error("File not found"); - file = null; - } - - if (file == null) - { - result = null; - } - else - { - result = new Users(); - - try - { - String line; - while ((line = file.readLine()) != null) - { - String[] tokens = line.split(":"); - - User user = new User(); - user.setLogin(tokens[0]); - user.setPassword(tokens[1]); - user.setUid((new Integer(tokens[2])).intValue()); - user.setGid((new Integer(tokens[3])).intValue()); - user.setRealName(tokens[4]); - user.setHomeDirectory(tokens[5]); - - // Manage the case of empty shell. - if (tokens.length == 7) - { - user.setShell(tokens[6]); - } - else - { - user.setShell(""); - } - - result.add(user); - } - - file.close(); - } - catch (java.io.IOException exception) - { - logger.error("Exception here."); - result = null; - } - } - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class EtcPasswdFileReader +{ + static private final Logger logger = LoggerFactory.getLogger(EtcPasswdFileReader.class); + + /** + * + */ + static public Users load() + { + Users result; + + BufferedReader file; + + try + { + file = new BufferedReader(new FileReader("/etc/passwd")); + } + catch (FileNotFoundException exception) + { + logger.error("File not found"); + file = null; + } + + if (file == null) + { + result = null; + } + else + { + result = new Users(); + + try + { + String line; + while ((line = file.readLine()) != null) + { + String[] tokens = line.split(":"); + + User user = new User(); + user.setLogin(tokens[0]); + user.setPassword(tokens[1]); + user.setUid((new Integer(tokens[2])).intValue()); + user.setGid((new Integer(tokens[3])).intValue()); + user.setRealName(tokens[4]); + user.setHomeDirectory(tokens[5]); + + // Manage the case of empty shell. + if (tokens.length == 7) + { + user.setShell(tokens[6]); + } + else + { + user.setShell(""); + } + + result.add(user); + } + + file.close(); + } + catch (java.io.IOException exception) + { + logger.error("Exception here."); + result = null; + } + } + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/Group.java b/src/fr/devinsy/util/unix/Group.java index 3b3c5a5..b237d8b 100644 --- a/src/fr/devinsy/util/unix/Group.java +++ b/src/fr/devinsy/util/unix/Group.java @@ -1,204 +1,204 @@ -package fr.devinsy.util.unix; - -import java.util.Vector; - -/** - * - */ -public class Group -{ - // static private final Logger logger = - // LoggerFactory.getLogger(Group.class); - - /* - * /usr/include/grp.h - * - * The group structure. - * struct group - * { - * char *gr_name; Group name. - * char *gr_passwd; Password. - * __gid_t gr_gid; Group ID. - * char **gr_mem; Member list. - * }; - */ - - protected String name; - protected String password; - protected int gid; - protected Vector members; - - /** - * - */ - public Group() - { - this.name = null; - this.password = null; - this.gid = -1; - this.members = new Vector(); - } - - /** - * - */ - public void addMember(final String login) - { - if ((login != null) && (login.length() != 0)) - { - this.members.add(login); - } - } - - /** - * - */ - public int getGid() - { - int result; - - result = this.gid; - - // - return (result); - } - - /** - * - */ - public Vector getMembers() - { - Vector result; - - result = this.members; - - // - return (result); - } - - /** - * - */ - public String getName() - { - String result; - - result = this.name; - - // - return (result); - } - - /** - * - */ - public String getPassword() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public int gid() - { - int result; - - result = this.gid; - - // - return (result); - } - - /** - * - */ - public Vector members() - { - Vector result; - - result = this.members; - - // - return (result); - } - - /** - * - */ - public String name() - { - String result; - - result = this.name; - - // - return (result); - } - - /** - * - */ - public String passwd() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public void setGid(final int gid) - { - this.gid = gid; - } - - /** - * - */ - public void setName(final String name) - { - this.name = name; - } - - /** - * - */ - public void setPasswd(final String password) - { - this.password = password; - } - - /** - * - */ - public void setPassword(final String password) - { - this.password = password; - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = "|" + this.name + "|" + this.password + "|" + this.gid + "|" + this.members + "|"; - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.util.Vector; + +/** + * + */ +public class Group +{ + // static private final Logger logger = + // LoggerFactory.getLogger(Group.class); + + /* + * /usr/include/grp.h + * + * The group structure. + * struct group + * { + * char *gr_name; Group name. + * char *gr_passwd; Password. + * __gid_t gr_gid; Group ID. + * char **gr_mem; Member list. + * }; + */ + + protected String name; + protected String password; + protected int gid; + protected Vector members; + + /** + * + */ + public Group() + { + this.name = null; + this.password = null; + this.gid = -1; + this.members = new Vector(); + } + + /** + * + */ + public void addMember(final String login) + { + if ((login != null) && (login.length() != 0)) + { + this.members.add(login); + } + } + + /** + * + */ + public int getGid() + { + int result; + + result = this.gid; + + // + return (result); + } + + /** + * + */ + public Vector getMembers() + { + Vector result; + + result = this.members; + + // + return (result); + } + + /** + * + */ + public String getName() + { + String result; + + result = this.name; + + // + return (result); + } + + /** + * + */ + public String getPassword() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public int gid() + { + int result; + + result = this.gid; + + // + return (result); + } + + /** + * + */ + public Vector members() + { + Vector result; + + result = this.members; + + // + return (result); + } + + /** + * + */ + public String name() + { + String result; + + result = this.name; + + // + return (result); + } + + /** + * + */ + public String passwd() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public void setGid(final int gid) + { + this.gid = gid; + } + + /** + * + */ + public void setName(final String name) + { + this.name = name; + } + + /** + * + */ + public void setPasswd(final String password) + { + this.password = password; + } + + /** + * + */ + public void setPassword(final String password) + { + this.password = password; + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = "|" + this.name + "|" + this.password + "|" + this.gid + "|" + this.members + "|"; + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/Groups.java b/src/fr/devinsy/util/unix/Groups.java index 5482f58..b98d9f3 100644 --- a/src/fr/devinsy/util/unix/Groups.java +++ b/src/fr/devinsy/util/unix/Groups.java @@ -1,214 +1,214 @@ -package fr.devinsy.util.unix; - -import java.util.Iterator; -import java.util.Vector; - -/** - * - */ -public class Groups extends Vector -{ - - private static final long serialVersionUID = 5802487312198869603L; - - // static private final Logger logger = - // LoggerFactory.getLogger(Groups.class); - - /** - * - */ - public Groups() - { - super(); - } - - /* - * - */ - public boolean contains(final int gid) - { - boolean result; - - if (getByGid(gid) == null) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /* - * - */ - public boolean contains(final String name) - { - boolean result; - - if (getByName(name) == null) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /** - * - */ - public Group getByGid(final int gid) - { - Group result; - - result = null; - boolean ended = false; - Iterator iterator = this.iterator(); - while (!ended) - { - if (iterator.hasNext()) - { - Group group = iterator.next(); - if (group.getGid() == gid) - { - ended = true; - result = group; - } - } - else - { - ended = true; - result = null; - } - } - - // - return (result); - } - - /** - * - */ - public Group getByName(final String name) - { - Group result; - - if (name == null) - { - result = null; - } - else - { - result = null; - boolean ended = false; - Iterator iterator = this.iterator(); - while (!ended) - { - if (iterator.hasNext()) - { - Group group = iterator.next(); - if (group.getName().equals(name)) - { - ended = true; - result = group; - } - } - else - { - ended = true; - result = null; - } - } - } - - // - return (result); - } - - /** - * - */ - public Vector getLoginGroups(final String login) - { - Vector result; - - result = new Vector(); - Iterator iterator = this.iterator(); - - while (iterator.hasNext()) - { - Group group = iterator.next(); - - if (group.members().contains(login)) - { - result.add(group.getName()); - } - } - - // - return (result); - } - - /** - * - */ - public String getLoginGroupsString(final String login) - { - String result; - - Vector groups = getLoginGroups(login); - - StringBuffer string = new StringBuffer(); - - for (String group : groups) - { - if (string.length() == 0) - { - string.append(group); - } - else - { - string.append(","); - string.append(group); - } - } - - result = string.toString(); - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - StringBuffer out = new StringBuffer(); - - Iterator iterator = this.iterator(); - - while (iterator.hasNext()) - { - out.append(iterator.next().toString() + "\n"); - } - - result = out.toString(); - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.util.Iterator; +import java.util.Vector; + +/** + * + */ +public class Groups extends Vector +{ + + private static final long serialVersionUID = 5802487312198869603L; + + // static private final Logger logger = + // LoggerFactory.getLogger(Groups.class); + + /** + * + */ + public Groups() + { + super(); + } + + /* + * + */ + public boolean contains(final int gid) + { + boolean result; + + if (getByGid(gid) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /* + * + */ + public boolean contains(final String name) + { + boolean result; + + if (getByName(name) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /** + * + */ + public Group getByGid(final int gid) + { + Group result; + + result = null; + boolean ended = false; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + Group group = iterator.next(); + if (group.getGid() == gid) + { + ended = true; + result = group; + } + } + else + { + ended = true; + result = null; + } + } + + // + return (result); + } + + /** + * + */ + public Group getByName(final String name) + { + Group result; + + if (name == null) + { + result = null; + } + else + { + result = null; + boolean ended = false; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + Group group = iterator.next(); + if (group.getName().equals(name)) + { + ended = true; + result = group; + } + } + else + { + ended = true; + result = null; + } + } + } + + // + return (result); + } + + /** + * + */ + public Vector getLoginGroups(final String login) + { + Vector result; + + result = new Vector(); + Iterator iterator = this.iterator(); + + while (iterator.hasNext()) + { + Group group = iterator.next(); + + if (group.members().contains(login)) + { + result.add(group.getName()); + } + } + + // + return (result); + } + + /** + * + */ + public String getLoginGroupsString(final String login) + { + String result; + + Vector groups = getLoginGroups(login); + + StringBuffer string = new StringBuffer(); + + for (String group : groups) + { + if (string.length() == 0) + { + string.append(group); + } + else + { + string.append(","); + string.append(group); + } + } + + result = string.toString(); + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer out = new StringBuffer(); + + Iterator iterator = this.iterator(); + + while (iterator.hasNext()) + { + out.append(iterator.next().toString() + "\n"); + } + + result = out.toString(); + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/Unix.java b/src/fr/devinsy/util/unix/Unix.java index 236e04c..b95972c 100644 --- a/src/fr/devinsy/util/unix/Unix.java +++ b/src/fr/devinsy/util/unix/Unix.java @@ -1,578 +1,578 @@ -package fr.devinsy.util.unix; - -import java.io.File; -import java.util.Vector; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import fr.devinsy.util.StringConcatenator; -import fr.devinsy.util.cmdexec.CmdExec; -import fr.devinsy.util.unix.acl.Acl; -import fr.devinsy.util.unix.acl.AclManager; - -/** - * - */ -public class Unix -{ - static private final Logger logger = LoggerFactory.getLogger(Unix.class); - static public final String SUDO = "/usr/bin/sudo"; - - /** - * - */ - static public void appendToFile(final String text, final String path) throws Exception - { - if ((text == null) || (text.length() == 0) || (path == null) || (path.length() == 0)) - { - throw new Exception("Parameter undefined: [" + text + "][" + path + "]."); - } - else - { - try - { - CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path); - } - catch (Exception exception) - { - throw new Exception("Error detected appending text to file [" + path + "].", exception); - } - } - } - - /** - * - */ - static public void chmod(final String changes, final String path) throws Exception - { - if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0)) - { - throw new Exception("Parameter undefined: [" + changes + "][" + path + "]."); - } - else if (!new File(path).exists()) - { - throw new Exception("Path not found: [" + path + "]."); - } - else - { - try - { - CmdExec.run(SUDO, "chmod", changes, path); - } - catch (Exception exception) - { - throw new Exception("Error running chmod command for [" + changes + "][" + path + "].", exception); - } - } - } - - /** - * - */ - static public void clearAcl(final String id, final String filePathName) throws Exception - { - AclManager.clearId(id, filePathName); - } - - /** - * - */ - static public void clearAclGroup(final String group, final String filePathName) throws Exception - { - AclManager.clearGroup(group, filePathName); - } - - /** - * - */ - static public void clearAclUser(final String login, final String filePathName) throws Exception - { - AclManager.clearUser(login, filePathName); - } - - /** - * - */ - static public void createUserAccount(final String login) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else - { - createUserAccount(login, login); - } - } - - /** - * - */ - static public void createUserAccount(final String login, final String name) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else if ((name == null) || (name.length() == 0)) - { - throw new Exception("Name parameter undefined."); - } - else - { - createUserAccount(login, name, "/home/" + login); - } - } - - /** - * - */ - static public void createUserAccount(final String login, final String name, final String home) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else if ((name == null) || (name.length() == 0)) - { - throw new Exception("Name parameter undefined."); - } - else if ((home == null) || (home.length() == 0)) - { - throw new Exception("Home parameter undefined."); - } - else - { - try - { - logger.info("Creating user account for [" + login + "]."); - CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name, "-d", home, login); - EtcPasswdFile.instance().update(); - logger.info("User account created for [" + login + "]."); - } - catch (Exception exception) - { - throw new Exception("Error detected creating user account [" + login + "].", exception); - } - } - } - - /** - * - */ - static public void createUserAccount(final String login, final String name, final String home, final String password) throws Exception - { - if ((password == null) || (password.length() == 0)) - { - throw new Exception("Password parameter undefined."); - } - else if (Unix.isLogin(login)) - { - throw new Exception("Login [" + login + "] already in use"); - } - else - { - createUserAccount(login, name, home); - setPassword(login, password); - } - } - - /** - * - */ - static public void deleteGroup(final String group) throws Exception - { - if ((group == null) || (group.length() == 0)) - { - throw new Exception("Group parameter undefined."); - } - else - { - try - { - logger.info("Deleting group for [" + group + "]."); - CmdExec.run(SUDO + " groupdel " + group); - logger.info("Group deleted for [" + group + "]."); - } - catch (Exception exception) - { - throw new Exception("Error running groupdel command for group [" + group + "].", exception); - } - } - } - - /** - * - */ - static public void deleteUserAccount(final String login) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else - { - try - { - logger.info("Deleting user account for [" + login + "]."); - CmdExec.run(SUDO + " /usr/sbin/userdel " + login); - logger.info("User account delted for [" + login + "]."); - } - catch (Exception exception) - { - throw new Exception("Error running userdel command for login [" + login + "].", exception); - } - } - } - - /** - * - */ - static public String getAclData(final String filePathName) throws Exception - { - String result; - - result = AclManager.getAclData(filePathName); - - // - return (result); - } - - /** - * - */ - static public String[] getAclUsers(final String filePathName) throws Exception - { - String[] result; - - Acl acl = AclManager.getAcl(filePathName); - - result = acl.currentAcl().getUserIds(); - - // - return (result); - } - - /** - * - */ - static public boolean isGroup(final String groupName) - { - boolean result; - - result = EtcGroupFile.instance().contains(groupName); - - // - return (result); - } - - /** - * - */ - static public boolean isLogin(final String login) - { - boolean result; - - result = EtcPasswdFile.instance().contains(login); - - // - return (result); - } - - /** - * - */ - static public void link(final String sourcePath, final String targetPath) throws Exception - { - logger.info("[" + sourcePath + "][" + targetPath + "]"); - if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0)) - { - throw new Exception("Parameter undefined: [" + sourcePath + "][" + targetPath + "]."); - } - else - { - File sourceFile = new File(sourcePath); - File targetFile = new File(targetPath); - - if (!sourceFile.exists()) - { - throw new Exception("Source does not exist: [" + sourcePath + "]."); - } - else if ((targetFile.exists()) && (!targetFile.isDirectory())) - { - throw new Exception("Target already exists: [" + targetPath + "]."); - } - else - { - try - { - CmdExec.run(SUDO, "ln", "-s", sourcePath, targetPath); - } - catch (Exception exception) - { - throw new Exception("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception); - } - } - } - } - - /** - * - */ - static public void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws Exception - { - logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); - if ((sourceLogin == null) || (sourceLogin.length() == 0)) - { - throw new Exception("Original login parameters undefined"); - } - else if ((targetLogin == null) || (targetLogin.length() == 0)) - { - throw new Exception("New login parameters undefined"); - } - else if (!Unix.isLogin(sourceLogin)) - { - throw new Exception("Original login unknow: [" + sourceLogin + "]."); - } - else if (Unix.isLogin(targetLogin)) - { - throw new Exception("New login unknow: [" + targetLogin + "]."); - } - else if (sourceHomeDirectory == null) - { - throw new Exception("sourceHomeDirectory parameter undefined, thus no home directory move."); - } - else if (!new File(sourceHomeDirectory).exists()) - { - throw new Exception("Source home directory does not exist: [" + sourceHomeDirectory + "]."); - } - else - { - String targetHomeDirectory = new File(sourceHomeDirectory).getParent().toString() + "/" + targetLogin; - if (new File(targetHomeDirectory).exists()) - { - throw new Exception("Target home directory already exists: [" + targetHomeDirectory + "]."); - } - else - { - try - { - logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); - CmdExec.run(SUDO + " usermod -l " + targetLogin + " " + sourceLogin); - logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]"); - } - catch (Exception exception) - { - throw new Exception("Login modification failed for [" + sourceLogin + "].", exception); - } - - try - { - logger.info("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); - CmdExec.run(SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory); - logger.info("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); - } - catch (Exception exception) - { - throw new Exception("Home directory rename failed for [" + sourceHomeDirectory + "].", exception); - } - } - } - logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]"); - } - - /** - * - */ - static public void recursiveChmod(final String changes, final String path) throws Exception - { - if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0)) - { - throw new Exception("Parameter undefined: [" + changes + "][" + path + "]."); - } - else if (!new File(path).exists()) - { - throw new Exception("Path not found: [" + path + "]."); - } - else - { - try - { - CmdExec.run(SUDO, "chmod", "-R", changes, path); - } - catch (Exception exception) - { - throw new Exception("Error running recursive chmod command for [" + changes + "][" + path + "].", exception); - } - } - } - - /** - * - */ - static public void renameGroup(final String sourceGroup, final String targetGroup) throws Exception - { - logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]"); - if ((sourceGroup == null) || (sourceGroup.length() == 0)) - { - throw new Exception("Original group name parameters undefined"); - } - else if ((targetGroup == null) || (targetGroup.length() == 0)) - { - throw new Exception("New group name parameters undefined"); - } - else if (!Unix.isGroup(sourceGroup)) - { - throw new Exception("Original group unknow: [" + sourceGroup + "]."); - } - else if (Unix.isGroup(targetGroup)) - { - throw new Exception("New group unknow: [" + targetGroup + "]."); - } - else - { - try - { - logger.info("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]"); - CmdExec.run(SUDO + " groupmod -n " + targetGroup + " " + sourceGroup); - logger.info("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]"); - } - catch (Exception exception) - { - throw new Exception("Group renaming failed for [" + sourceGroup + "].", exception); - } - } - logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]"); - } - - /** - * - */ - static public Group searchGroup(final String groupName) - { - Group result; - - result = EtcGroupFile.instance().get(groupName); - - // - return (result); - } - - /** - * - */ - static public User searchLogin(final String login) - { - User result; - - result = EtcPasswdFile.instance().get(login); - - // - return (result); - } - - /** - * - */ - static public Vector searchLoginGroups(final String login) - { - Vector result; - - result = EtcGroupFile.instance().getLoginGroups(login); - - // - return (result); - } - - /** - * - */ - static public void setfacl(final String... args) throws Exception - { - try - { - CmdExec.run(SUDO, "setfacl", args, 1, 6); - } - catch (Exception exception) - { - throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception); - } - } - - /** - * As 'passwd' command has not the option '--stdin' in all systems (eg. - * Debian), this method uses the 'chpasswd' command. - */ - static public void setPassword(final String login, final String newPassword) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else if (newPassword == null) - { - throw new Exception("New password parameter undefined."); - } - else - { - try - { - logger.info("Password setting for [" + login + "]."); - CmdExec.run(SUDO, "bash", "-c", "echo \"" + login + ":" + newPassword + "\"| chpasswd "); - logger.info("Password set for [" + login + "]."); - } - catch (Exception exception) - { - throw new Exception("Error detected on setting of the new password for [" + login + "].", exception); - } - } - } - - /** - * chfn [ -f full-name ] [ username ] - */ - static public void setRealName(final String login, final String newRealName) throws Exception - { - if ((login == null) || (login.length() == 0)) - { - throw new Exception("Login parameter undefined."); - } - else if (newRealName == null) - { - throw new Exception("New real name parameter undefined."); - } - else - { - try - { - logger.info("Real name changing for user [" + login + "]."); - CmdExec.run(SUDO, "chfn", "-f", newRealName, login); - EtcPasswdFile.instance().update(); - logger.info("Real name changed for user [" + login + "]."); - } - catch (Exception exception) - { - throw new Exception("Error detected on setting of the new real name for [" + login + "].", exception); - } - } - } - - /** - * - */ - static public void unlink(final String filePathName) throws Exception - { - logger.info("[" + filePathName + "]"); - if (filePathName == null) - { - throw new Exception("Parameter undefined: [" + filePathName + "]."); - } - else - { - new File(filePathName).delete(); - } - } -} - +package fr.devinsy.util.unix; + +import java.io.File; +import java.util.Vector; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.util.StringConcatenator; +import fr.devinsy.util.cmdexec.CmdExec; +import fr.devinsy.util.unix.acl.Acl; +import fr.devinsy.util.unix.acl.AclManager; + +/** + * + */ +public class Unix +{ + static private final Logger logger = LoggerFactory.getLogger(Unix.class); + static public final String SUDO = "/usr/bin/sudo"; + + /** + * + */ + static public void appendToFile(final String text, final String path) throws Exception + { + if ((text == null) || (text.length() == 0) || (path == null) || (path.length() == 0)) + { + throw new Exception("Parameter undefined: [" + text + "][" + path + "]."); + } + else + { + try + { + CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path); + } + catch (Exception exception) + { + throw new Exception("Error detected appending text to file [" + path + "].", exception); + } + } + } + + /** + * + */ + static public void chmod(final String changes, final String path) throws Exception + { + if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0)) + { + throw new Exception("Parameter undefined: [" + changes + "][" + path + "]."); + } + else if (!new File(path).exists()) + { + throw new Exception("Path not found: [" + path + "]."); + } + else + { + try + { + CmdExec.run(SUDO, "chmod", changes, path); + } + catch (Exception exception) + { + throw new Exception("Error running chmod command for [" + changes + "][" + path + "].", exception); + } + } + } + + /** + * + */ + static public void clearAcl(final String id, final String filePathName) throws Exception + { + AclManager.clearId(id, filePathName); + } + + /** + * + */ + static public void clearAclGroup(final String group, final String filePathName) throws Exception + { + AclManager.clearGroup(group, filePathName); + } + + /** + * + */ + static public void clearAclUser(final String login, final String filePathName) throws Exception + { + AclManager.clearUser(login, filePathName); + } + + /** + * + */ + static public void createUserAccount(final String login) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else + { + createUserAccount(login, login); + } + } + + /** + * + */ + static public void createUserAccount(final String login, final String name) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else if ((name == null) || (name.length() == 0)) + { + throw new Exception("Name parameter undefined."); + } + else + { + createUserAccount(login, name, "/home/" + login); + } + } + + /** + * + */ + static public void createUserAccount(final String login, final String name, final String home) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else if ((name == null) || (name.length() == 0)) + { + throw new Exception("Name parameter undefined."); + } + else if ((home == null) || (home.length() == 0)) + { + throw new Exception("Home parameter undefined."); + } + else + { + try + { + logger.info("Creating user account for [" + login + "]."); + CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name, "-d", home, login); + EtcPasswdFile.instance().update(); + logger.info("User account created for [" + login + "]."); + } + catch (Exception exception) + { + throw new Exception("Error detected creating user account [" + login + "].", exception); + } + } + } + + /** + * + */ + static public void createUserAccount(final String login, final String name, final String home, final String password) throws Exception + { + if ((password == null) || (password.length() == 0)) + { + throw new Exception("Password parameter undefined."); + } + else if (Unix.isLogin(login)) + { + throw new Exception("Login [" + login + "] already in use"); + } + else + { + createUserAccount(login, name, home); + setPassword(login, password); + } + } + + /** + * + */ + static public void deleteGroup(final String group) throws Exception + { + if ((group == null) || (group.length() == 0)) + { + throw new Exception("Group parameter undefined."); + } + else + { + try + { + logger.info("Deleting group for [" + group + "]."); + CmdExec.run(SUDO + " groupdel " + group); + logger.info("Group deleted for [" + group + "]."); + } + catch (Exception exception) + { + throw new Exception("Error running groupdel command for group [" + group + "].", exception); + } + } + } + + /** + * + */ + static public void deleteUserAccount(final String login) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else + { + try + { + logger.info("Deleting user account for [" + login + "]."); + CmdExec.run(SUDO + " /usr/sbin/userdel " + login); + logger.info("User account delted for [" + login + "]."); + } + catch (Exception exception) + { + throw new Exception("Error running userdel command for login [" + login + "].", exception); + } + } + } + + /** + * + */ + static public String getAclData(final String filePathName) throws Exception + { + String result; + + result = AclManager.getAclData(filePathName); + + // + return (result); + } + + /** + * + */ + static public String[] getAclUsers(final String filePathName) throws Exception + { + String[] result; + + Acl acl = AclManager.getAcl(filePathName); + + result = acl.currentAcl().getUserIds(); + + // + return (result); + } + + /** + * + */ + static public boolean isGroup(final String groupName) + { + boolean result; + + result = EtcGroupFile.instance().contains(groupName); + + // + return (result); + } + + /** + * + */ + static public boolean isLogin(final String login) + { + boolean result; + + result = EtcPasswdFile.instance().contains(login); + + // + return (result); + } + + /** + * + */ + static public void link(final String sourcePath, final String targetPath) throws Exception + { + logger.info("[" + sourcePath + "][" + targetPath + "]"); + if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0)) + { + throw new Exception("Parameter undefined: [" + sourcePath + "][" + targetPath + "]."); + } + else + { + File sourceFile = new File(sourcePath); + File targetFile = new File(targetPath); + + if (!sourceFile.exists()) + { + throw new Exception("Source does not exist: [" + sourcePath + "]."); + } + else if ((targetFile.exists()) && (!targetFile.isDirectory())) + { + throw new Exception("Target already exists: [" + targetPath + "]."); + } + else + { + try + { + CmdExec.run(SUDO, "ln", "-s", sourcePath, targetPath); + } + catch (Exception exception) + { + throw new Exception("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception); + } + } + } + } + + /** + * + */ + static public void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws Exception + { + logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); + if ((sourceLogin == null) || (sourceLogin.length() == 0)) + { + throw new Exception("Original login parameters undefined"); + } + else if ((targetLogin == null) || (targetLogin.length() == 0)) + { + throw new Exception("New login parameters undefined"); + } + else if (!Unix.isLogin(sourceLogin)) + { + throw new Exception("Original login unknow: [" + sourceLogin + "]."); + } + else if (Unix.isLogin(targetLogin)) + { + throw new Exception("New login unknow: [" + targetLogin + "]."); + } + else if (sourceHomeDirectory == null) + { + throw new Exception("sourceHomeDirectory parameter undefined, thus no home directory move."); + } + else if (!new File(sourceHomeDirectory).exists()) + { + throw new Exception("Source home directory does not exist: [" + sourceHomeDirectory + "]."); + } + else + { + String targetHomeDirectory = new File(sourceHomeDirectory).getParent().toString() + "/" + targetLogin; + if (new File(targetHomeDirectory).exists()) + { + throw new Exception("Target home directory already exists: [" + targetHomeDirectory + "]."); + } + else + { + try + { + logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); + CmdExec.run(SUDO + " usermod -l " + targetLogin + " " + sourceLogin); + logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]"); + } + catch (Exception exception) + { + throw new Exception("Login modification failed for [" + sourceLogin + "].", exception); + } + + try + { + logger.info("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); + CmdExec.run(SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory); + logger.info("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); + } + catch (Exception exception) + { + throw new Exception("Home directory rename failed for [" + sourceHomeDirectory + "].", exception); + } + } + } + logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]"); + } + + /** + * + */ + static public void recursiveChmod(final String changes, final String path) throws Exception + { + if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0)) + { + throw new Exception("Parameter undefined: [" + changes + "][" + path + "]."); + } + else if (!new File(path).exists()) + { + throw new Exception("Path not found: [" + path + "]."); + } + else + { + try + { + CmdExec.run(SUDO, "chmod", "-R", changes, path); + } + catch (Exception exception) + { + throw new Exception("Error running recursive chmod command for [" + changes + "][" + path + "].", exception); + } + } + } + + /** + * + */ + static public void renameGroup(final String sourceGroup, final String targetGroup) throws Exception + { + logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]"); + if ((sourceGroup == null) || (sourceGroup.length() == 0)) + { + throw new Exception("Original group name parameters undefined"); + } + else if ((targetGroup == null) || (targetGroup.length() == 0)) + { + throw new Exception("New group name parameters undefined"); + } + else if (!Unix.isGroup(sourceGroup)) + { + throw new Exception("Original group unknow: [" + sourceGroup + "]."); + } + else if (Unix.isGroup(targetGroup)) + { + throw new Exception("New group unknow: [" + targetGroup + "]."); + } + else + { + try + { + logger.info("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]"); + CmdExec.run(SUDO + " groupmod -n " + targetGroup + " " + sourceGroup); + logger.info("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]"); + } + catch (Exception exception) + { + throw new Exception("Group renaming failed for [" + sourceGroup + "].", exception); + } + } + logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]"); + } + + /** + * + */ + static public Group searchGroup(final String groupName) + { + Group result; + + result = EtcGroupFile.instance().get(groupName); + + // + return (result); + } + + /** + * + */ + static public User searchLogin(final String login) + { + User result; + + result = EtcPasswdFile.instance().get(login); + + // + return (result); + } + + /** + * + */ + static public Vector searchLoginGroups(final String login) + { + Vector result; + + result = EtcGroupFile.instance().getLoginGroups(login); + + // + return (result); + } + + /** + * + */ + static public void setfacl(final String... args) throws Exception + { + try + { + CmdExec.run(SUDO, "setfacl", args, 1, 6); + } + catch (Exception exception) + { + throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception); + } + } + + /** + * As 'passwd' command has not the option '--stdin' in all systems (eg. + * Debian), this method uses the 'chpasswd' command. + */ + static public void setPassword(final String login, final String newPassword) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else if (newPassword == null) + { + throw new Exception("New password parameter undefined."); + } + else + { + try + { + logger.info("Password setting for [" + login + "]."); + CmdExec.run(SUDO, "bash", "-c", "echo \"" + login + ":" + newPassword + "\"| chpasswd "); + logger.info("Password set for [" + login + "]."); + } + catch (Exception exception) + { + throw new Exception("Error detected on setting of the new password for [" + login + "].", exception); + } + } + } + + /** + * chfn [ -f full-name ] [ username ] + */ + static public void setRealName(final String login, final String newRealName) throws Exception + { + if ((login == null) || (login.length() == 0)) + { + throw new Exception("Login parameter undefined."); + } + else if (newRealName == null) + { + throw new Exception("New real name parameter undefined."); + } + else + { + try + { + logger.info("Real name changing for user [" + login + "]."); + CmdExec.run(SUDO, "chfn", "-f", newRealName, login); + EtcPasswdFile.instance().update(); + logger.info("Real name changed for user [" + login + "]."); + } + catch (Exception exception) + { + throw new Exception("Error detected on setting of the new real name for [" + login + "].", exception); + } + } + } + + /** + * + */ + static public void unlink(final String filePathName) throws Exception + { + logger.info("[" + filePathName + "]"); + if (filePathName == null) + { + throw new Exception("Parameter undefined: [" + filePathName + "]."); + } + else + { + new File(filePathName).delete(); + } + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/User.java b/src/fr/devinsy/util/unix/User.java index 358ccd9..ddb56bc 100644 --- a/src/fr/devinsy/util/unix/User.java +++ b/src/fr/devinsy/util/unix/User.java @@ -1,337 +1,337 @@ -package fr.devinsy.util.unix; - - -/** - * - */ -public class User -{ - // static private final Logger logger = LoggerFactory.getLogger(User.class); - - /* - * /usr/include/pwd.h - * - * The passwd structure. - * struct passwd - * { - * char *pw_name;D Username. - * char *pw_passwd; Password. - * __uid_t pw_uid; User ID. - * __gid_t pw_gid; Group ID. - * char *pw_gecos; Real name. - * char *pw_dir; Home directory. - * char *pw_shell; Shell program. - * }; - */ - - protected String login; - protected String password; - protected int uid; - protected int gid; - protected String realName; - protected String homeDirectory; - protected String shell; - - /** - * - */ - public User() - { - this.login = null; - this.password = null; - this.uid = -1; - this.gid = -1; - this.realName = null; - this.homeDirectory = null; - this.shell = null; - } - - /** - * - */ - public User(final User user) - { - this.login = user.login(); - this.password = user.passwd(); - this.uid = user.uid(); - this.gid = user.gid(); - this.realName = user.realName(); - this.homeDirectory = user.homeDirectory(); - this.shell = user.shell(); - } - - /** - * - */ - public int getGid() - { - int result; - - result = this.gid; - - // - return (result); - } - - /** - * - */ - public String getHomeDirectory() - { - String result; - - result = this.homeDirectory; - - // - return (result); - } - - /** - * - */ - public String getLogin() - { - String result; - - result = this.login; - - // - return (result); - } - - /** - * - */ - public String getPasswd() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public String getPassword() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public String getRealName() - { - String result; - - result = this.realName; - - // - return (result); - } - - /** - * - */ - public String getShell() - { - String result; - - result = this.shell; - - // - return (result); - } - - /** - * - */ - public int getUid() - { - int result; - - result = this.uid; - - // - return (result); - } - - /** - * - */ - public int gid() - { - int result; - - result = this.gid; - - // - return (result); - } - - /** - * - */ - public String homeDirectory() - { - String result; - - result = this.homeDirectory; - - // - return (result); - } - - /** - * - */ - public String login() - { - String result; - - result = this.login; - - // - return (result); - } - - /** - * - */ - public String passwd() - { - String result; - - result = this.password; - - // - return (result); - } - - /** - * - */ - public String realName() - { - String result; - - result = this.realName; - - // - return (result); - } - - /** - * - */ - public void setGid(final int gid) - { - this.gid = gid; - } - - /** - * - */ - public void setHomeDirectory(final String homeDirectory) - { - this.homeDirectory = homeDirectory; - } - - /** - * - */ - public void setLogin(final String login) - { - this.login = login; - } - - /** - * - */ - public void setPasswd(final String password) - { - this.password = password; - } - - /** - * - */ - public void setPassword(final String password) - { - this.password = password; - } - - /** - * - */ - public void setRealName(final String realName) - { - this.realName = realName; - } - - /** - * - */ - public void setShell(final String shell) - { - this.shell = shell; - } - - /** - * - */ - public void setUid(final int uid) - { - this.uid = uid; - } - - /** - * - */ - public String shell() - { - String result; - - result = this.shell; - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = "|" + this.login + "|" + this.password + "|" + this.uid + "|" + this.gid + "|" + this.realName + "|" + this.homeDirectory + "|" + this.shell + "|"; - - // - return (result); - } - - /** - * - */ - public int uid() - { - int result; - - result = this.uid; - - // - return (result); - } -} - +package fr.devinsy.util.unix; + + +/** + * + */ +public class User +{ + // static private final Logger logger = LoggerFactory.getLogger(User.class); + + /* + * /usr/include/pwd.h + * + * The passwd structure. + * struct passwd + * { + * char *pw_name;D Username. + * char *pw_passwd; Password. + * __uid_t pw_uid; User ID. + * __gid_t pw_gid; Group ID. + * char *pw_gecos; Real name. + * char *pw_dir; Home directory. + * char *pw_shell; Shell program. + * }; + */ + + protected String login; + protected String password; + protected int uid; + protected int gid; + protected String realName; + protected String homeDirectory; + protected String shell; + + /** + * + */ + public User() + { + this.login = null; + this.password = null; + this.uid = -1; + this.gid = -1; + this.realName = null; + this.homeDirectory = null; + this.shell = null; + } + + /** + * + */ + public User(final User user) + { + this.login = user.login(); + this.password = user.passwd(); + this.uid = user.uid(); + this.gid = user.gid(); + this.realName = user.realName(); + this.homeDirectory = user.homeDirectory(); + this.shell = user.shell(); + } + + /** + * + */ + public int getGid() + { + int result; + + result = this.gid; + + // + return (result); + } + + /** + * + */ + public String getHomeDirectory() + { + String result; + + result = this.homeDirectory; + + // + return (result); + } + + /** + * + */ + public String getLogin() + { + String result; + + result = this.login; + + // + return (result); + } + + /** + * + */ + public String getPasswd() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public String getPassword() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public String getRealName() + { + String result; + + result = this.realName; + + // + return (result); + } + + /** + * + */ + public String getShell() + { + String result; + + result = this.shell; + + // + return (result); + } + + /** + * + */ + public int getUid() + { + int result; + + result = this.uid; + + // + return (result); + } + + /** + * + */ + public int gid() + { + int result; + + result = this.gid; + + // + return (result); + } + + /** + * + */ + public String homeDirectory() + { + String result; + + result = this.homeDirectory; + + // + return (result); + } + + /** + * + */ + public String login() + { + String result; + + result = this.login; + + // + return (result); + } + + /** + * + */ + public String passwd() + { + String result; + + result = this.password; + + // + return (result); + } + + /** + * + */ + public String realName() + { + String result; + + result = this.realName; + + // + return (result); + } + + /** + * + */ + public void setGid(final int gid) + { + this.gid = gid; + } + + /** + * + */ + public void setHomeDirectory(final String homeDirectory) + { + this.homeDirectory = homeDirectory; + } + + /** + * + */ + public void setLogin(final String login) + { + this.login = login; + } + + /** + * + */ + public void setPasswd(final String password) + { + this.password = password; + } + + /** + * + */ + public void setPassword(final String password) + { + this.password = password; + } + + /** + * + */ + public void setRealName(final String realName) + { + this.realName = realName; + } + + /** + * + */ + public void setShell(final String shell) + { + this.shell = shell; + } + + /** + * + */ + public void setUid(final int uid) + { + this.uid = uid; + } + + /** + * + */ + public String shell() + { + String result; + + result = this.shell; + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = "|" + this.login + "|" + this.password + "|" + this.uid + "|" + this.gid + "|" + this.realName + "|" + this.homeDirectory + "|" + this.shell + "|"; + + // + return (result); + } + + /** + * + */ + public int uid() + { + int result; + + result = this.uid; + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/Users.java b/src/fr/devinsy/util/unix/Users.java index 6ac7609..8bbf878 100644 --- a/src/fr/devinsy/util/unix/Users.java +++ b/src/fr/devinsy/util/unix/Users.java @@ -1,160 +1,160 @@ -package fr.devinsy.util.unix; - -import java.util.Iterator; -import java.util.Vector; - -/** - * - */ -public class Users extends Vector -{ - private static final long serialVersionUID = -7178304512851592399L; - - // static private final Logger logger = - // LoggerFactory.getLogger(Users.class); - - /** - * - */ - public Users() - { - super(); - } - - /* - * - */ - public boolean contains(final int uid) - { - boolean result; - - if (getByUid(uid) == null) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /* - * - */ - public boolean contains(final String login) - { - boolean result; - - if (getByLogin(login) == null) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /** - * - */ - public User getByLogin(final String login) - { - User result; - - if (login == null) - { - result = null; - } - else - { - result = null; - boolean ended = false; - Iterator iterator = this.iterator(); - while (!ended) - { - if (iterator.hasNext()) - { - User user = iterator.next(); - if (user.getLogin().equals(login)) - { - ended = true; - result = user; - } - } - else - { - ended = true; - result = null; - } - } - } - - // - return (result); - } - - /** - * - */ - public User getByUid(final int uid) - { - User result; - - result = null; - boolean ended = false; - Iterator iterator = this.iterator(); - while (!ended) - { - if (iterator.hasNext()) - { - User user = iterator.next(); - if (user.getUid() == uid) - { - ended = true; - result = user; - } - } - else - { - ended = true; - result = null; - } - } - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - StringBuffer out; - out = new StringBuffer(); - - Iterator iterator = this.iterator(); - - while (iterator.hasNext()) - { - out.append(iterator.next().toString() + "\n"); - } - - result = out.toString(); - - // - return (result); - } -} - +package fr.devinsy.util.unix; + +import java.util.Iterator; +import java.util.Vector; + +/** + * + */ +public class Users extends Vector +{ + private static final long serialVersionUID = -7178304512851592399L; + + // static private final Logger logger = + // LoggerFactory.getLogger(Users.class); + + /** + * + */ + public Users() + { + super(); + } + + /* + * + */ + public boolean contains(final int uid) + { + boolean result; + + if (getByUid(uid) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /* + * + */ + public boolean contains(final String login) + { + boolean result; + + if (getByLogin(login) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /** + * + */ + public User getByLogin(final String login) + { + User result; + + if (login == null) + { + result = null; + } + else + { + result = null; + boolean ended = false; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + User user = iterator.next(); + if (user.getLogin().equals(login)) + { + ended = true; + result = user; + } + } + else + { + ended = true; + result = null; + } + } + } + + // + return (result); + } + + /** + * + */ + public User getByUid(final int uid) + { + User result; + + result = null; + boolean ended = false; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + User user = iterator.next(); + if (user.getUid() == uid) + { + ended = true; + result = user; + } + } + else + { + ended = true; + result = null; + } + } + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer out; + out = new StringBuffer(); + + Iterator iterator = this.iterator(); + + while (iterator.hasNext()) + { + out.append(iterator.next().toString() + "\n"); + } + + result = out.toString(); + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/acl/Acl.java b/src/fr/devinsy/util/unix/acl/Acl.java index 7426ac5..7b521c0 100644 --- a/src/fr/devinsy/util/unix/acl/Acl.java +++ b/src/fr/devinsy/util/unix/acl/Acl.java @@ -1,197 +1,197 @@ -package fr.devinsy.util.unix.acl; - -/** - * - */ -public class Acl -{ - // static private final Logger logger = LoggerFactory.getLogger(Acl.class); - - /* - # file: goo39 - # owner: goo39 - # group: goo39 - user::rwx - user:cpm:rwx #effective:rwx - user:goo39:rwx #effective:rwx - group::--- #effective:--- - group:goo40:rwx #effective:rwx - mask::rwx - other::--- - default:user::rwx - default:user:cpm:rwx #effective:rwx - default:group::--- #effective:--- - default:group:cpm:rwx #effective:rwx - default:group:goo40:rwx #effective:rwx - default:mask::rwx - default:other::--- - */ - - protected String filePathname; - protected String owner; - protected String group; - - protected AclEntries currentAcl; - protected AclEntries defaultAcl; - - /** - * - */ - public Acl(final String filePathname) - { - this.filePathname = filePathname; - this.owner = ""; - this.group = ""; - this.currentAcl = new AclEntries(); - this.defaultAcl = new AclEntries(); - } - - /** - * - */ - public boolean contains(final AclEntry.Type type, final String id) - { - boolean result; - - if ((this.currentAcl().contains(type, id)) || (this.defaultAcl().contains(type, id))) - { - result = true; - } - else - { - result = false; - } - - // - return (result); - } - - /** - * - */ - public boolean containsGroup(final String group) - { - boolean result; - - result = contains(AclEntry.Type.GROUP, group); - - // - return (result); - } - - /** - * - */ - public boolean containsId(final String id) - { - boolean result; - - if ((containsUser(id) || containsGroup(id))) - { - result = true; - } - else - { - result = false; - } - - // - return (result); - } - - /** - * - */ - public boolean containsUser(final String login) - { - boolean result; - - result = contains(AclEntry.Type.USER, login); - - // - return (result); - } - - /** - * - */ - public AclEntries currentAcl() - { - AclEntries result; - - result = this.currentAcl; - - // - return (result); - } - - /** - * - */ - public AclEntries defaultAcl() - { - AclEntries result; - - result = this.defaultAcl; - - // - return (result); - } - - /** - * - */ - public String filePathname() - { - String result; - - result = this.filePathname; - - // - return (result); - } - - /** - * - */ - public String group() - { - String result; - - result = this.group; - - // - return (result); - } - - /** - * - */ - public String owner() - { - String result; - - result = this.owner; - - // - return (result); - } - - /** - * - */ - public void setGroup(final String group) - { - this.group = group; - } - - /** - * - */ - public void setOwner(final String owner) - { - this.owner = owner; - } -} - +package fr.devinsy.util.unix.acl; + +/** + * + */ +public class Acl +{ + // static private final Logger logger = LoggerFactory.getLogger(Acl.class); + + /* + # file: goo39 + # owner: goo39 + # group: goo39 + user::rwx + user:cpm:rwx #effective:rwx + user:goo39:rwx #effective:rwx + group::--- #effective:--- + group:goo40:rwx #effective:rwx + mask::rwx + other::--- + default:user::rwx + default:user:cpm:rwx #effective:rwx + default:group::--- #effective:--- + default:group:cpm:rwx #effective:rwx + default:group:goo40:rwx #effective:rwx + default:mask::rwx + default:other::--- + */ + + protected String filePathname; + protected String owner; + protected String group; + + protected AclEntries currentAcl; + protected AclEntries defaultAcl; + + /** + * + */ + public Acl(final String filePathname) + { + this.filePathname = filePathname; + this.owner = ""; + this.group = ""; + this.currentAcl = new AclEntries(); + this.defaultAcl = new AclEntries(); + } + + /** + * + */ + public boolean contains(final AclEntry.Type type, final String id) + { + boolean result; + + if ((this.currentAcl().contains(type, id)) || (this.defaultAcl().contains(type, id))) + { + result = true; + } + else + { + result = false; + } + + // + return (result); + } + + /** + * + */ + public boolean containsGroup(final String group) + { + boolean result; + + result = contains(AclEntry.Type.GROUP, group); + + // + return (result); + } + + /** + * + */ + public boolean containsId(final String id) + { + boolean result; + + if ((containsUser(id) || containsGroup(id))) + { + result = true; + } + else + { + result = false; + } + + // + return (result); + } + + /** + * + */ + public boolean containsUser(final String login) + { + boolean result; + + result = contains(AclEntry.Type.USER, login); + + // + return (result); + } + + /** + * + */ + public AclEntries currentAcl() + { + AclEntries result; + + result = this.currentAcl; + + // + return (result); + } + + /** + * + */ + public AclEntries defaultAcl() + { + AclEntries result; + + result = this.defaultAcl; + + // + return (result); + } + + /** + * + */ + public String filePathname() + { + String result; + + result = this.filePathname; + + // + return (result); + } + + /** + * + */ + public String group() + { + String result; + + result = this.group; + + // + return (result); + } + + /** + * + */ + public String owner() + { + String result; + + result = this.owner; + + // + return (result); + } + + /** + * + */ + public void setGroup(final String group) + { + this.group = group; + } + + /** + * + */ + public void setOwner(final String owner) + { + this.owner = owner; + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/acl/AclEntries.java b/src/fr/devinsy/util/unix/acl/AclEntries.java index 957f4bb..6360eec 100644 --- a/src/fr/devinsy/util/unix/acl/AclEntries.java +++ b/src/fr/devinsy/util/unix/acl/AclEntries.java @@ -1,197 +1,197 @@ -package fr.devinsy.util.unix.acl; - -import java.util.Iterator; -import java.util.Vector; - -/** - * - */ -public class AclEntries extends Vector -{ - private static final long serialVersionUID = 5802487312198869603L; - - // static private final Logger logger = - // LoggerFactory.getLogger(AclEntries.class); - - /** - * - */ - public AclEntries() - { - super(); - } - - /* - * - */ - public boolean contains(final AclEntry.Type type, final String id) - { - boolean result; - - if (this.get(type, id) == null) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /** - * - */ - public boolean containsId(final String id) - { - boolean result; - - if ((this.get(AclEntry.Type.USER, id) == null) && (this.get(AclEntry.Type.GROUP, id) == null)) - { - result = false; - } - else - { - result = true; - } - - // - return (result); - } - - /** - * - */ - public AclEntry get(final AclEntry.Type type, final String id) - { - AclEntry result; - - result = null; - boolean ended = false; - Iterator iterator = this.iterator(); - while (!ended) - { - if (iterator.hasNext()) - { - AclEntry entry = iterator.next(); - if ((type == entry.type()) && (entry.id().equals(id))) - { - ended = true; - result = entry; - } - } - else - { - ended = true; - result = null; - } - } - - // - return (result); - } - - /** - * - */ - public String[] getGroupIds() - { - String[] result; - - Vector ids = new Vector(); - - for (AclEntry entry : this) - { - if ((entry.type == AclEntry.Type.GROUP) && (!ids.contains(entry.id()))) - { - ids.add(entry.id()); - } - } - - result = (String[]) ids.toArray(); - - // - return (result); - } - - /** - * - */ - public String[] getIds() - { - String[] result; - - Vector ids = new Vector(); - - for (AclEntry entry : this) - { - if (!ids.contains(entry.id())) - { - ids.add(entry.id()); - } - } - - result = (String[]) ids.toArray(); - - // - return (result); - } - - /** - * - */ - public String[] getUserIds() - { - String[] result; - - // - Vector ids = new Vector(); - - for (AclEntry entry : this) - { - if ((entry.type == AclEntry.Type.USER) && (!ids.contains(entry.id()))) - { - ids.add(entry.id()); - } - } - - // - result = new String[ids.size()]; - - for (int nId = 0; nId < ids.size(); nId++) - { - result[nId] = ids.get(nId); - } - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - StringBuffer out; - out = new StringBuffer(); - - Iterator iterator = this.iterator(); - - while (iterator.hasNext()) - { - out.append(iterator.next().toString() + "\n"); - } - - result = out.toString(); - - // - return (result); - } -} - +package fr.devinsy.util.unix.acl; + +import java.util.Iterator; +import java.util.Vector; + +/** + * + */ +public class AclEntries extends Vector +{ + private static final long serialVersionUID = 5802487312198869603L; + + // static private final Logger logger = + // LoggerFactory.getLogger(AclEntries.class); + + /** + * + */ + public AclEntries() + { + super(); + } + + /* + * + */ + public boolean contains(final AclEntry.Type type, final String id) + { + boolean result; + + if (this.get(type, id) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /** + * + */ + public boolean containsId(final String id) + { + boolean result; + + if ((this.get(AclEntry.Type.USER, id) == null) && (this.get(AclEntry.Type.GROUP, id) == null)) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /** + * + */ + public AclEntry get(final AclEntry.Type type, final String id) + { + AclEntry result; + + result = null; + boolean ended = false; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + AclEntry entry = iterator.next(); + if ((type == entry.type()) && (entry.id().equals(id))) + { + ended = true; + result = entry; + } + } + else + { + ended = true; + result = null; + } + } + + // + return (result); + } + + /** + * + */ + public String[] getGroupIds() + { + String[] result; + + Vector ids = new Vector(); + + for (AclEntry entry : this) + { + if ((entry.type == AclEntry.Type.GROUP) && (!ids.contains(entry.id()))) + { + ids.add(entry.id()); + } + } + + result = (String[]) ids.toArray(); + + // + return (result); + } + + /** + * + */ + public String[] getIds() + { + String[] result; + + Vector ids = new Vector(); + + for (AclEntry entry : this) + { + if (!ids.contains(entry.id())) + { + ids.add(entry.id()); + } + } + + result = (String[]) ids.toArray(); + + // + return (result); + } + + /** + * + */ + public String[] getUserIds() + { + String[] result; + + // + Vector ids = new Vector(); + + for (AclEntry entry : this) + { + if ((entry.type == AclEntry.Type.USER) && (!ids.contains(entry.id()))) + { + ids.add(entry.id()); + } + } + + // + result = new String[ids.size()]; + + for (int nId = 0; nId < ids.size(); nId++) + { + result[nId] = ids.get(nId); + } + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + StringBuffer out; + out = new StringBuffer(); + + Iterator iterator = this.iterator(); + + while (iterator.hasNext()) + { + out.append(iterator.next().toString() + "\n"); + } + + result = out.toString(); + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/acl/AclEntry.java b/src/fr/devinsy/util/unix/acl/AclEntry.java index 69b41f1..b9771fa 100644 --- a/src/fr/devinsy/util/unix/acl/AclEntry.java +++ b/src/fr/devinsy/util/unix/acl/AclEntry.java @@ -1,95 +1,95 @@ -package fr.devinsy.util.unix.acl; - - -/** - * - */ -public class AclEntry -{ - // static private final Logger logger = - // LoggerFactory.getLogger(AclEntry.class); - - /* - user::rwx - user:cpm:rwx #effective:rwx - user:goo39:rwx #effective:rwx - group::--- #effective:--- - group:goo40:rwx #effective:rwx - mask::rwx - other::--- - */ - - public enum Type - { - NONE, USER, GROUP, MASK, OTHER - }; - - protected Type type; - protected String id; - protected String permission; - - /** - * - */ - public AclEntry(final Type type, final String id, final String permission) - { - this.type = type; - this.id = id; - this.permission = permission; - } - - /** - * - */ - public String id() - { - String result; - - result = this.id; - - // - return (result); - } - - /** - * - */ - public String permission() - { - String result; - - result = this.type.toString() + ":" + this.id + ":" + this.permission; - - // - return (result); - } - - /** - * - */ - @Override - public String toString() - { - String result; - - result = permission; - - // - return (result); - } - - /** - * - */ - public Type type() - { - Type result; - - result = this.type; - - // - return (result); - } -} - +package fr.devinsy.util.unix.acl; + + +/** + * + */ +public class AclEntry +{ + // static private final Logger logger = + // LoggerFactory.getLogger(AclEntry.class); + + /* + user::rwx + user:cpm:rwx #effective:rwx + user:goo39:rwx #effective:rwx + group::--- #effective:--- + group:goo40:rwx #effective:rwx + mask::rwx + other::--- + */ + + public enum Type + { + NONE, USER, GROUP, MASK, OTHER + }; + + protected Type type; + protected String id; + protected String permission; + + /** + * + */ + public AclEntry(final Type type, final String id, final String permission) + { + this.type = type; + this.id = id; + this.permission = permission; + } + + /** + * + */ + public String id() + { + String result; + + result = this.id; + + // + return (result); + } + + /** + * + */ + public String permission() + { + String result; + + result = this.type.toString() + ":" + this.id + ":" + this.permission; + + // + return (result); + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = permission; + + // + return (result); + } + + /** + * + */ + public Type type() + { + Type result; + + result = this.type; + + // + return (result); + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/util/unix/acl/AclManager.java b/src/fr/devinsy/util/unix/acl/AclManager.java index 82f0638..5619926 100644 --- a/src/fr/devinsy/util/unix/acl/AclManager.java +++ b/src/fr/devinsy/util/unix/acl/AclManager.java @@ -1,405 +1,405 @@ -package fr.devinsy.util.unix.acl; - -import java.io.File; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import fr.devinsy.util.StringConcatenator; -import fr.devinsy.util.cmdexec.CmdExec; -import fr.devinsy.util.unix.Unix; - -/** - * - */ -public class AclManager -{ - static private final Logger logger = LoggerFactory.getLogger(AclManager.class); - static public final String SUDO = "/usr/bin/sudo"; - - static final public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$"); - static final public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$"); - static final public Pattern MASK_PATTERN = Pattern.compile("^mask:(.*):(.*)$"); - 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_GROUP_PATTERN = Pattern.compile("^default:group:(.*):(.*)$"); - static final public Pattern DEFAULT_MASK_PATTERN = Pattern.compile("^default:mask:(.*):(.*)$"); - static final public Pattern DEFAULT_OTHER_PATTERN = Pattern.compile("^default:other:(.*):(.*)$"); - - /** - * - */ - public static void clearGroup(final String group, final String filePathName) throws Exception - { - if ((group == null) || (group.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) - { - throw new Exception("Bad parameters [" + group + "][" + filePathName + "]."); - } - else - { - Unix.setfacl("-R", "-L", "-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 - { - clearUser(id, filePathName); - clearGroup(id, filePathName); - } - - /** - * - */ - public static void clearUser(final String login, final String filePathName) throws Exception - { - if ((login == null) || (login.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) - { - throw new Exception("Bad parameters [" + login + "][" + filePathName + "]."); - } - else - { - Unix.setfacl("-R", "-L", "-x", "user:" + login, filePathName); - Unix.setfacl("-R", "-L", "-d", "-x", "user:" + login, filePathName); - } - } - - /** - * - */ - static public Acl getAcl(final String filePathName) throws Exception - { - Acl result; - - result = new Acl(filePathName); - - String[] entries = getAclEntryLines(filePathName); - - // Login pattern: "^[a-z_][a-z0-9_-]*$". - logger.debug("Line=[" + entries[1] + "]"); - Matcher matcher = Pattern.compile("^#\\sowner:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[1]); - if (matcher.matches()) - { - logger.debug("group=[" + matcher.group(1) + "]"); - result.setOwner(matcher.group(1)); - } - - // Group pattern: "^[a-z_][a-z0-9_-]*$". - logger.debug("Line=[" + entries[2] + "]"); - matcher = Pattern.compile("^#\\sgroup:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[2]); - if (matcher.matches()) - { - logger.debug("group=[" + matcher.group(1) + "]"); - result.setOwner(matcher.group(1)); - } - - for (int nEntry = 3; nEntry < entries.length; nEntry++) - { - String entryLine = entries[nEntry]; - logger.debug("Line=[" + entryLine + "]"); - - // - Matcher userMatcher = USER_PATTERN.matcher(entryLine); - Matcher groupMatcher = GROUP_PATTERN.matcher(entryLine); - Matcher maskMatcher = MASK_PATTERN.matcher(entryLine); - Matcher otherMatcher = OTHER_PATTERN.matcher(entryLine); - Matcher defaultUserMatcher = DEFAULT_USER_PATTERN.matcher(entryLine); - Matcher defaultGroupMatcher = DEFAULT_GROUP_PATTERN.matcher(entryLine); - Matcher defaultMaskMatcher = DEFAULT_MASK_PATTERN.matcher(entryLine); - Matcher defaultOtherMatcher = DEFAULT_OTHER_PATTERN.matcher(entryLine); - - AclEntry entry; - if (userMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.USER, userMatcher.group(1), userMatcher.group(2)); - result.currentAcl().add(entry); - } - else if (groupMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.GROUP, groupMatcher.group(1), groupMatcher.group(2)); - result.currentAcl().add(entry); - } - else if (maskMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.MASK, maskMatcher.group(1), maskMatcher.group(2)); - result.currentAcl().add(entry); - } - else if (otherMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.OTHER, otherMatcher.group(1), otherMatcher.group(2)); - result.currentAcl().add(entry); - } - else if (defaultUserMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.USER, defaultUserMatcher.group(1), defaultUserMatcher.group(2)); - result.defaultAcl().add(entry); - } - else if (defaultGroupMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.GROUP, defaultGroupMatcher.group(1), defaultGroupMatcher.group(2)); - result.defaultAcl().add(entry); - } - else if (defaultMaskMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.MASK, defaultMaskMatcher.group(1), defaultMaskMatcher.group(2)); - result.defaultAcl().add(entry); - } - else if (defaultOtherMatcher.matches()) - { - entry = new AclEntry(AclEntry.Type.OTHER, defaultOtherMatcher.group(1), defaultOtherMatcher.group(2)); - result.defaultAcl().add(entry); - } - else - { - throw new Exception("Unknow ACL entry line pattern for [" + entryLine + "]."); - } - - logger.debug("Acl entry decoded: [" + entry.toString() + "]"); - } - - // - return (result); - } - - /** - * - */ - static public String getAclData(final String filePathName) throws Exception - { - String result; - - try - { - logger.info("Getting Acl data for [" + filePathName + "]."); - result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName); - logger.info("Acl data got for [" + filePathName + "]."); - } - catch (Exception exception) - { - throw new Exception("Error getting ACL for [" + filePathName + "].", exception); - } - - // - return (result); - } - - /** - * - */ - static public String[] getAclEntryLines(final String filePathName) - { - String[] result; - - try - { - result = getAclData(filePathName).split("\n"); - } - catch (Exception exception) - { - result = new String[0]; - } - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws Exception - { - boolean result; - - result = isUsed(type, id, filePathName, 0); - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws Exception - { - boolean result; - - if ((type == null) || (id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) - { - throw new Exception("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "]."); - } - else - { - File file = new File(filePathName); - if (!file.exists()) - { - throw new Exception("File does not exist [" + filePathName + "]."); - } - else - { - Acl acl = getAcl(filePathName); - if (acl.contains(type, id)) - { - result = true; - } - else if ((file.isDirectory()) && (depth > 0)) - { - result = isUsed(type, id, filePathName, file.list(), depth - 1); - } - else - { - result = false; - } - } - } - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws Exception - { - boolean result; - - result = false; - boolean ended = false; - int nLine = 0; - while (!ended) - { - if (nLine < filePathNames.length) - { - String filePathName = filePathNames[nLine]; - if (isUsed(type, id, filePath + "/" + filePathName, depth)) - { - ended = true; - result = true; - } - else - { - nLine += 1; - } - } - else - { - ended = true; - result = false; - } - } - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final String id, final String filePathName) throws Exception - { - boolean result; - - result = isUsed(id, filePathName, 0); - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final String id, final String filePathName, final int depth) throws Exception - { - boolean result; - - if ((id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) - { - throw new Exception("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "]."); - } - else - { - File file = new File(filePathName); - if (!file.exists()) - { - throw new Exception("File does not exist [" + filePathName + "]."); - } - else - { - Acl acl = getAcl(filePathName); - if (acl.containsId(id)) - { - result = true; - } - else if ((file.isDirectory()) && (depth > 0)) - { - result = isUsed(id, file.list(), depth - 1); - } - else - { - result = false; - } - } - } - - // - return (result); - } - - /** - * - */ - public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws Exception - { - boolean result; - - result = false; - boolean ended = false; - int nLine = 0; - while (!ended) - { - if (nLine < filePathNames.length) - { - String filePathName = filePathNames[nLine]; - if (isUsed(id, filePathName, depth)) - { - ended = true; - result = true; - } - else - { - nLine += 1; - } - } - else - { - ended = true; - result = false; - } - } - - // - return (result); - } - - /** - * - */ - static public void setfacl(final String... args) throws Exception - { - try - { - CmdExec.run(SUDO, "setfacl", args, 1, 5); - } - catch (Exception exception) - { - throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + "."); - } - } -} - +package fr.devinsy.util.unix.acl; + +import java.io.File; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.util.StringConcatenator; +import fr.devinsy.util.cmdexec.CmdExec; +import fr.devinsy.util.unix.Unix; + +/** + * + */ +public class AclManager +{ + static private final Logger logger = LoggerFactory.getLogger(AclManager.class); + static public final String SUDO = "/usr/bin/sudo"; + + static final public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$"); + static final public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$"); + static final public Pattern MASK_PATTERN = Pattern.compile("^mask:(.*):(.*)$"); + 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_GROUP_PATTERN = Pattern.compile("^default:group:(.*):(.*)$"); + static final public Pattern DEFAULT_MASK_PATTERN = Pattern.compile("^default:mask:(.*):(.*)$"); + static final public Pattern DEFAULT_OTHER_PATTERN = Pattern.compile("^default:other:(.*):(.*)$"); + + /** + * + */ + public static void clearGroup(final String group, final String filePathName) throws Exception + { + if ((group == null) || (group.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) + { + throw new Exception("Bad parameters [" + group + "][" + filePathName + "]."); + } + else + { + Unix.setfacl("-R", "-L", "-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 + { + clearUser(id, filePathName); + clearGroup(id, filePathName); + } + + /** + * + */ + public static void clearUser(final String login, final String filePathName) throws Exception + { + if ((login == null) || (login.length() == 0) || (filePathName == null) || (filePathName.length() == 0)) + { + throw new Exception("Bad parameters [" + login + "][" + filePathName + "]."); + } + else + { + Unix.setfacl("-R", "-L", "-x", "user:" + login, filePathName); + Unix.setfacl("-R", "-L", "-d", "-x", "user:" + login, filePathName); + } + } + + /** + * + */ + static public Acl getAcl(final String filePathName) throws Exception + { + Acl result; + + result = new Acl(filePathName); + + String[] entries = getAclEntryLines(filePathName); + + // Login pattern: "^[a-z_][a-z0-9_-]*$". + logger.debug("Line=[" + entries[1] + "]"); + Matcher matcher = Pattern.compile("^#\\sowner:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[1]); + if (matcher.matches()) + { + logger.debug("group=[" + matcher.group(1) + "]"); + result.setOwner(matcher.group(1)); + } + + // Group pattern: "^[a-z_][a-z0-9_-]*$". + logger.debug("Line=[" + entries[2] + "]"); + matcher = Pattern.compile("^#\\sgroup:\\s([a-z_][a-z0-9_-]*)$").matcher(entries[2]); + if (matcher.matches()) + { + logger.debug("group=[" + matcher.group(1) + "]"); + result.setOwner(matcher.group(1)); + } + + for (int nEntry = 3; nEntry < entries.length; nEntry++) + { + String entryLine = entries[nEntry]; + logger.debug("Line=[" + entryLine + "]"); + + // + Matcher userMatcher = USER_PATTERN.matcher(entryLine); + Matcher groupMatcher = GROUP_PATTERN.matcher(entryLine); + Matcher maskMatcher = MASK_PATTERN.matcher(entryLine); + Matcher otherMatcher = OTHER_PATTERN.matcher(entryLine); + Matcher defaultUserMatcher = DEFAULT_USER_PATTERN.matcher(entryLine); + Matcher defaultGroupMatcher = DEFAULT_GROUP_PATTERN.matcher(entryLine); + Matcher defaultMaskMatcher = DEFAULT_MASK_PATTERN.matcher(entryLine); + Matcher defaultOtherMatcher = DEFAULT_OTHER_PATTERN.matcher(entryLine); + + AclEntry entry; + if (userMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.USER, userMatcher.group(1), userMatcher.group(2)); + result.currentAcl().add(entry); + } + else if (groupMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.GROUP, groupMatcher.group(1), groupMatcher.group(2)); + result.currentAcl().add(entry); + } + else if (maskMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.MASK, maskMatcher.group(1), maskMatcher.group(2)); + result.currentAcl().add(entry); + } + else if (otherMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.OTHER, otherMatcher.group(1), otherMatcher.group(2)); + result.currentAcl().add(entry); + } + else if (defaultUserMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.USER, defaultUserMatcher.group(1), defaultUserMatcher.group(2)); + result.defaultAcl().add(entry); + } + else if (defaultGroupMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.GROUP, defaultGroupMatcher.group(1), defaultGroupMatcher.group(2)); + result.defaultAcl().add(entry); + } + else if (defaultMaskMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.MASK, defaultMaskMatcher.group(1), defaultMaskMatcher.group(2)); + result.defaultAcl().add(entry); + } + else if (defaultOtherMatcher.matches()) + { + entry = new AclEntry(AclEntry.Type.OTHER, defaultOtherMatcher.group(1), defaultOtherMatcher.group(2)); + result.defaultAcl().add(entry); + } + else + { + throw new Exception("Unknow ACL entry line pattern for [" + entryLine + "]."); + } + + logger.debug("Acl entry decoded: [" + entry.toString() + "]"); + } + + // + return (result); + } + + /** + * + */ + static public String getAclData(final String filePathName) throws Exception + { + String result; + + try + { + logger.info("Getting Acl data for [" + filePathName + "]."); + result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName); + logger.info("Acl data got for [" + filePathName + "]."); + } + catch (Exception exception) + { + throw new Exception("Error getting ACL for [" + filePathName + "].", exception); + } + + // + return (result); + } + + /** + * + */ + static public String[] getAclEntryLines(final String filePathName) + { + String[] result; + + try + { + result = getAclData(filePathName).split("\n"); + } + catch (Exception exception) + { + result = new String[0]; + } + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws Exception + { + boolean result; + + result = isUsed(type, id, filePathName, 0); + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws Exception + { + boolean result; + + if ((type == null) || (id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) + { + throw new Exception("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "]."); + } + else + { + File file = new File(filePathName); + if (!file.exists()) + { + throw new Exception("File does not exist [" + filePathName + "]."); + } + else + { + Acl acl = getAcl(filePathName); + if (acl.contains(type, id)) + { + result = true; + } + else if ((file.isDirectory()) && (depth > 0)) + { + result = isUsed(type, id, filePathName, file.list(), depth - 1); + } + else + { + result = false; + } + } + } + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws Exception + { + boolean result; + + result = false; + boolean ended = false; + int nLine = 0; + while (!ended) + { + if (nLine < filePathNames.length) + { + String filePathName = filePathNames[nLine]; + if (isUsed(type, id, filePath + "/" + filePathName, depth)) + { + ended = true; + result = true; + } + else + { + nLine += 1; + } + } + else + { + ended = true; + result = false; + } + } + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final String id, final String filePathName) throws Exception + { + boolean result; + + result = isUsed(id, filePathName, 0); + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final String id, final String filePathName, final int depth) throws Exception + { + boolean result; + + if ((id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0)) + { + throw new Exception("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "]."); + } + else + { + File file = new File(filePathName); + if (!file.exists()) + { + throw new Exception("File does not exist [" + filePathName + "]."); + } + else + { + Acl acl = getAcl(filePathName); + if (acl.containsId(id)) + { + result = true; + } + else if ((file.isDirectory()) && (depth > 0)) + { + result = isUsed(id, file.list(), depth - 1); + } + else + { + result = false; + } + } + } + + // + return (result); + } + + /** + * + */ + public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws Exception + { + boolean result; + + result = false; + boolean ended = false; + int nLine = 0; + while (!ended) + { + if (nLine < filePathNames.length) + { + String filePathName = filePathNames[nLine]; + if (isUsed(id, filePathName, depth)) + { + ended = true; + result = true; + } + else + { + nLine += 1; + } + } + else + { + ended = true; + result = false; + } + } + + // + return (result); + } + + /** + * + */ + static public void setfacl(final String... args) throws Exception + { + try + { + CmdExec.run(SUDO, "setfacl", args, 1, 5); + } + catch (Exception exception) + { + throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + "."); + } + } +} + // //////////////////////////////////////////////////////////////////////// \ No newline at end of file