- ps.println("</tomcat-users>");
- ps.flush();
- } catch (Throwable t) {
- t.printStackTrace(ps);
- return false;
- }
- return true;
- }
-
- /**
- *
- * Note: This method returns a String if there's an error, or null if ok.
- * This unusual style is necessitated by the fact that any Exceptions thrown are likely to
- * be unlogged and hidden from view, making debugging almost impossible.
- *
- * @param writeto
- * @param up
- * @return
- */
- public static String updateUsers(String writeto, LocalLur up) {
- // Dump a Tomcat-user.xml lookalike (anywhere)
- if(writeto!=null) {
- // First read content
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- if(UsersDump.write(baos, up)) {
- byte[] postulate = baos.toByteArray();
- // now get contents of file
- File file = new File(writeto);
- boolean writeIt;
- if(file.exists()) {
- try {
- FileInputStream fis = new FileInputStream(file);
- byte[] orig = new byte[(int)file.length()];
- try {
- fis.read(orig);
- } finally {
- fis.close();
- }
- // Starting at third "<" (<tomcat-users> line)
- int startA=0, startB=0;
- for(int i=0;startA<orig.length && i<3;++startA) if(orig[startA]=='<')++i;
- for(int i=0;startB<orig.length && i<3;++startB) if(postulate[startB]=='<')++i;
-
- writeIt=orig.length-startA!=postulate.length-startB; // first, check if remaining length is the same
- while(!writeIt && startA<orig.length && startB<postulate.length) {
- if(orig[startA++]!=postulate[startB++])writeIt = true;
- }
- } catch (Exception e) {
- writeIt = true;
- }
- } else {
- writeIt = true;
- }
-
- if(writeIt) {
- try {
- FileOutputStream fos = new FileOutputStream(file);
- try {
- fos.write(postulate);
- } finally {
- fos.close();
- }
- } catch (IOException e) {
- return e.getMessage();
- }
- }
- }
- }
- return null; // no message means ok.
- }