/protocols/ss7/sgw/boot/src/main/java/org/mobicents/ss7/sgw/boot/Configuration.java
Java | 159 lines | 98 code | 29 blank | 32 comment | 5 complexity | 4161caf8b28ced4a75789a24c20ad996 MD5 | raw file
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 23package org.mobicents.ss7.sgw.boot; 24 25import java.io.ByteArrayOutputStream; 26import java.io.File; 27import java.io.FileInputStream; 28import java.io.FileNotFoundException; 29import java.io.FileOutputStream; 30import java.io.IOException; 31import java.net.MalformedURLException; 32import java.net.URL; 33 34import org.apache.log4j.Logger; 35/** 36 * 37 * @author kulikov 38 */ 39public class Configuration { 40 41 private final static String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n " + 42 "<deployment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 43 "xsi:schemaLocation=\"urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd\" " + 44 "xmlns=\"urn:jboss:bean-deployer:2.0\">\n"; 45 46 private final static String FOOTER = "</deployment>"; 47 48 private File file; 49 private File directory; 50 51 private boolean isDirectory; 52 private long lastModified; 53 54 private Logger logger = Logger.getLogger(Configuration.class); 55 56 public Configuration(String s, File directory) { 57 this.directory = directory; 58 } 59 60 public String getBeans() throws IOException { 61 String config = ""; 62 File[] files = directory.listFiles(); 63 for (File file : files) { 64 if (!file.isDirectory()) { 65 logger.info("Configuring " + file); 66 String description = read(file); 67 68 int p1 = description.indexOf("<xml"); 69 int p2 = description.indexOf('>', p1); 70 71 description = description.substring(p2 + 1); 72 73 p1 = description.indexOf("<deployment"); 74 p2 = description.indexOf('>', p1); 75 76 description = description.substring(p2 + 1); 77 78 description = description.replaceFirst("</deployment>", ""); 79 config += "\n" + description; 80 } else { 81 logger.info("Reading directory " + file); 82 Configuration conf = new Configuration("",file); 83 config += conf.getBeans(); 84 } 85 } 86 return config; 87 } 88 89 public URL getConfig() throws IOException { 90 String s = HEADER + getBeans() + FOOTER; 91 92 //creating temp dir on one level top 93 File tempDir = new File(directory.getParent() + File.separator + "temp"); 94 tempDir.mkdir(); 95 96 //creating aggegated deployement descriptor 97 String temp = tempDir.getPath() + File.separator + "deployment-beans.xml"; 98 99 //write descriptor 100 FileOutputStream fout = new FileOutputStream(temp, false); 101 fout.write(s.getBytes()); 102 fout.flush(); 103 fout.close(); 104 105 File deployment = new File(temp); 106 return deployment.toURI().toURL(); 107 } 108 109 private String read(File file) throws IOException { 110 //create local buffer 111 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 112 113 //opening file 114 FileInputStream fin = null; 115 try { 116 fin = new FileInputStream(file); 117 } catch (FileNotFoundException e) { 118 } 119 120 //fetching data from file to local buffer 121 try { 122 int b = -1; 123 while ((b = fin.read()) != -1) { 124 bout.write(b); 125 } 126 } finally { 127 fin.close(); 128 } 129 130 //creating string 131 return new String(bout.toByteArray()); 132 } 133 134 public Configuration(File file) { 135 this.file = file; 136 this.isDirectory = file.isDirectory(); 137 this.lastModified = file.lastModified(); 138 } 139 140 public boolean isDirectory() { 141 return this.isDirectory; 142 } 143 144 public URL getURL() { 145 try { 146 return file.toURI().toURL(); 147 } catch (MalformedURLException e) { 148 return null; 149 } 150 } 151 152 public long lastModified() { 153 return lastModified; 154 } 155 156 public void update(long lastModified) { 157 this.lastModified = lastModified; 158 } 159}