PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/ss7/sgw/boot/src/main/java/org/mobicents/ss7/sgw/boot/Configuration.java

http://mobicents.googlecode.com/
Java | 159 lines | 98 code | 29 blank | 32 comment | 5 complexity | 4161caf8b28ced4a75789a24c20ad996 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * 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.mobicents.ss7.sgw.boot;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.net.MalformedURLException;
  30. import java.net.URL;
  31. import org.apache.log4j.Logger;
  32. /**
  33. *
  34. * @author kulikov
  35. */
  36. public class Configuration {
  37. private final static String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n " +
  38. "<deployment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
  39. "xsi:schemaLocation=\"urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd\" " +
  40. "xmlns=\"urn:jboss:bean-deployer:2.0\">\n";
  41. private final static String FOOTER = "</deployment>";
  42. private File file;
  43. private File directory;
  44. private boolean isDirectory;
  45. private long lastModified;
  46. private Logger logger = Logger.getLogger(Configuration.class);
  47. public Configuration(String s, File directory) {
  48. this.directory = directory;
  49. }
  50. public String getBeans() throws IOException {
  51. String config = "";
  52. File[] files = directory.listFiles();
  53. for (File file : files) {
  54. if (!file.isDirectory()) {
  55. logger.info("Configuring " + file);
  56. String description = read(file);
  57. int p1 = description.indexOf("<xml");
  58. int p2 = description.indexOf('>', p1);
  59. description = description.substring(p2 + 1);
  60. p1 = description.indexOf("<deployment");
  61. p2 = description.indexOf('>', p1);
  62. description = description.substring(p2 + 1);
  63. description = description.replaceFirst("</deployment>", "");
  64. config += "\n" + description;
  65. } else {
  66. logger.info("Reading directory " + file);
  67. Configuration conf = new Configuration("",file);
  68. config += conf.getBeans();
  69. }
  70. }
  71. return config;
  72. }
  73. public URL getConfig() throws IOException {
  74. String s = HEADER + getBeans() + FOOTER;
  75. //creating temp dir on one level top
  76. File tempDir = new File(directory.getParent() + File.separator + "temp");
  77. tempDir.mkdir();
  78. //creating aggegated deployement descriptor
  79. String temp = tempDir.getPath() + File.separator + "deployment-beans.xml";
  80. //write descriptor
  81. FileOutputStream fout = new FileOutputStream(temp, false);
  82. fout.write(s.getBytes());
  83. fout.flush();
  84. fout.close();
  85. File deployment = new File(temp);
  86. return deployment.toURI().toURL();
  87. }
  88. private String read(File file) throws IOException {
  89. //create local buffer
  90. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  91. //opening file
  92. FileInputStream fin = null;
  93. try {
  94. fin = new FileInputStream(file);
  95. } catch (FileNotFoundException e) {
  96. }
  97. //fetching data from file to local buffer
  98. try {
  99. int b = -1;
  100. while ((b = fin.read()) != -1) {
  101. bout.write(b);
  102. }
  103. } finally {
  104. fin.close();
  105. }
  106. //creating string
  107. return new String(bout.toByteArray());
  108. }
  109. public Configuration(File file) {
  110. this.file = file;
  111. this.isDirectory = file.isDirectory();
  112. this.lastModified = file.lastModified();
  113. }
  114. public boolean isDirectory() {
  115. return this.isDirectory;
  116. }
  117. public URL getURL() {
  118. try {
  119. return file.toURI().toURL();
  120. } catch (MalformedURLException e) {
  121. return null;
  122. }
  123. }
  124. public long lastModified() {
  125. return lastModified;
  126. }
  127. public void update(long lastModified) {
  128. this.lastModified = lastModified;
  129. }
  130. }