FileResolverImpl.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.corba.se.impl.resolver ; 27 28import org.omg.CORBA.ORBPackage.InvalidName; 29 30import com.sun.corba.se.spi.resolver.Resolver ; 31 32import java.util.Enumeration; 33import java.util.Properties; 34import java.util.Set; 35import java.util.HashSet; 36 37import java.io.File; 38import java.io.FileInputStream; 39 40import com.sun.corba.se.spi.orb.ORB ; 41 42import com.sun.corba.se.impl.orbutil.CorbaResourceUtil ; 43 44public class FileResolverImpl implements Resolver 45{ 46 private ORB orb ; 47 private File file ; 48 private Properties savedProps ; 49 private long fileModified = 0 ; 50 51 public FileResolverImpl( ORB orb, File file ) 52 { 53 this.orb = orb ; 54 this.file = file ; 55 savedProps = new Properties() ; 56 } 57 58 public org.omg.CORBA.Object resolve( String name ) 59 { 60 check() ; 61 String stringifiedObject = savedProps.getProperty( name ) ; 62 if (stringifiedObject == null) { 63 return null; 64 } 65 return orb.string_to_object( stringifiedObject ) ; 66 } 67 68 public java.util.Set list() 69 { 70 check() ; 71 72 Set result = new HashSet() ; 73 74 // Obtain all the keys from the property object 75 Enumeration theKeys = savedProps.propertyNames(); 76 while (theKeys.hasMoreElements()) { 77 result.add( theKeys.nextElement() ) ; 78 } 79 80 return result ; 81 } 82 83 /** 84 * Checks the lastModified() timestamp of the file and optionally 85 * re-reads the Properties object from the file if newer. 86 */ 87 private void check() 88 { 89 if (file == null) 90 return; 91 92 long lastMod = file.lastModified(); 93 if (lastMod > fileModified) { 94 try { 95 FileInputStream fileIS = new FileInputStream(file); 96 savedProps.clear(); 97 savedProps.load(fileIS); 98 fileIS.close(); 99 fileModified = lastMod; 100 } catch (java.io.FileNotFoundException e) { 101 System.err.println( CorbaResourceUtil.getText( 102 "bootstrap.filenotfound", file.getAbsolutePath())); 103 } catch (java.io.IOException e) { 104 System.err.println( CorbaResourceUtil.getText( 105 "bootstrap.exception", 106 file.getAbsolutePath(), e.toString())); 107 } 108 } 109 } 110} 111