PageRenderTime 41ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/services/path/AbstractPathService.java

#
Java | 114 lines | 55 code | 17 blank | 42 comment | 25 complexity | befb16b62f62d04dfcf0b1aad776e74e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.controller.services.path;
  23. import java.io.File;
  24. import org.jboss.msc.service.Service;
  25. import org.jboss.msc.service.ServiceName;
  26. import org.jboss.msc.service.StartContext;
  27. import org.jboss.msc.service.StartException;
  28. import org.jboss.msc.service.StopContext;
  29. /**
  30. * Abstract superclass for services that return a path.
  31. *
  32. * @author Brian Stansberry
  33. */
  34. public abstract class AbstractPathService implements Service<String> {
  35. private static final ServiceName SERVICE_NAME_BASE = ServiceName.JBOSS.append("server", "path");
  36. public static ServiceName pathNameOf(String pathName) {
  37. if (pathName == null) {
  38. throw new IllegalArgumentException("pathName is null");
  39. }
  40. return SERVICE_NAME_BASE.append(pathName);
  41. }
  42. /**
  43. * Checks whether the given path looks like an absolute Unix or Windows filesystem pathname <strong>without
  44. * regard for what the filesystem is underlying the Java Virtual Machine</strong>. A UNIX pathname is
  45. * absolute if its prefix is <code>"/"</code>. A Microsoft Windows pathname is absolute if its prefix is a drive
  46. * specifier followed by <code>"\\"</code>, or if its prefix is <code>"\\\\"</code>.
  47. * <p>
  48. * <strong>This method differs from simply creating a new {@code File} and calling {@link File#isAbsolute()} in that
  49. * its results do not change depending on what the filesystem underlying the Java Virtual Machine is. </strong>
  50. * </p>
  51. *
  52. * @param path the path
  53. *
  54. * @return {@code true} if {@code path} looks like an absolute Unix or Windows pathname
  55. */
  56. public static boolean isAbsoluteUnixOrWindowsPath(final String path) {
  57. if (path != null) {
  58. int length = path.length();
  59. if (length > 0) {
  60. char c0 = path.charAt(0);
  61. if (c0 == '/') {
  62. return true; // Absolute Unix path
  63. } else if (length > 1) {
  64. char c1 = path.charAt(1);
  65. if (c0 == '\\' && c1 == '\\') {
  66. return true; // Absolute UNC pathname "\\\\foo"
  67. } else if (length > 2 && c1 == ':' && path.charAt(2) == '\\' && isDriveLetter(c0) ) {
  68. return true; // Absolute local pathname "z:\\foo"
  69. }
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. private static boolean isDriveLetter(char c) {
  76. return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
  77. }
  78. private String path;
  79. // ------------------------------------------------------------ Service
  80. @Override
  81. public void start(StartContext context) throws StartException {
  82. path = resolvePath();
  83. }
  84. @Override
  85. public void stop(StopContext context) {
  86. }
  87. @Override
  88. public String getValue() throws IllegalStateException {
  89. final String path = this.path;
  90. if(path == null) {
  91. throw new IllegalStateException();
  92. }
  93. return path;
  94. }
  95. // ------------------------------------------------------------ Protected
  96. protected abstract String resolvePath();
  97. }