| 1 | package dmg.cells.nucleus ; |
| 2 | |
| 3 | |
| 4 | import java.io.* ; |
| 5 | import java.net.* ; |
| 6 | import java.util.* ; |
| 7 | import java.lang.reflect.* ; |
| 8 | |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | |
| 12 | public class CellUrl { |
| 13 | |
| 14 | private final static Logger _log = |
| 15 | LoggerFactory.getLogger(CellUrl.class); |
| 16 | |
| 17 | private CellGlue _glue = null ; |
| 18 | private URLStreamHandlerFactory _others = null ; |
| 19 | |
| 20 | public CellUrl( CellGlue glue ){ |
| 21 | |
| 22 | java.util.Properties p = System.getProperties(); |
| 23 | String s = p.getProperty("java.protocol.handler.pkgs"); |
| 24 | if(s != null) |
| 25 | { |
| 26 | s= s+"|dmg.cells.nucleus.protocols"; |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | s="dmg.cells.nucleus.protocols"; |
| 31 | } |
| 32 | |
| 33 | p.setProperty("java.protocol.handler.pkgs",s); |
| 34 | System.setProperties(p); |
| 35 | } |
| 36 | |
| 37 | public static class DomainUrlConnection extends URLConnection |
| 38 | { |
| 39 | private String _protocol = null ; |
| 40 | private CellNucleus _nucleus = null ; |
| 41 | private Map<String,Object> _environment = null ; |
| 42 | public DomainUrlConnection( URL url , String protocol ){ |
| 43 | super( url ) ; |
| 44 | _protocol = protocol ; |
| 45 | } |
| 46 | |
| 47 | public void connect(){ |
| 48 | |
| 49 | _log.info( "DomainUrlConnection : Connect called" ) ; |
| 50 | return ; |
| 51 | } |
| 52 | public void setNucleus( CellNucleus nucleus ){ |
| 53 | _nucleus = nucleus ; |
| 54 | } |
| 55 | |
| 56 | public void setEnvironment(Map<String,Object> environment ){ |
| 57 | _environment = environment ; |
| 58 | } |
| 59 | public InputStream getInputStream() throws IOException { |
| 60 | if( _nucleus == null ) |
| 61 | throw new IOException( "Nucleus not defined" ) ; |
| 62 | |
| 63 | throw new IOException( "getInputStream not supported on : "+_protocol ) ; |
| 64 | /* |
| 65 | if( _protocol.equals( "context" ) ){ |
| 66 | |
| 67 | }else if( _protocol.equals( "env" ) ){ |
| 68 | |
| 69 | }else if( _protocol.equals( "cell" ) ){ |
| 70 | |
| 71 | }else |
| 72 | throw new IOException( "Protocol not supported : "+_protocol ) ; |
| 73 | */ |
| 74 | } |
| 75 | public Reader getReader() throws IOException { |
| 76 | if( _nucleus == null ) |
| 77 | throw new IOException( "Nucleus not defined" ) ; |
| 78 | |
| 79 | if( _protocol.equals( "context" ) ){ |
| 80 | if( url.getHost().equals("") ){ |
| 81 | String filePart = url.getFile() ; |
| 82 | filePart = ( filePart.length() > 0 ) && |
| 83 | ( filePart.charAt(0) == '/' ) ? |
| 84 | filePart.substring(1) : |
| 85 | filePart ; |
| 86 | return |
| 87 | _nucleus.getDomainContextReader( filePart ) ; |
| 88 | }else |
| 89 | return getRemoteContextReader( _nucleus , url ) ; |
| 90 | }else if( _protocol.equals( "env" ) ){ |
| 91 | if( _environment == null ) |
| 92 | throw new IOException( "Nucleus not defined" ) ; |
| 93 | |
| 94 | String filePart = url.getFile() ; |
| 95 | filePart = ( filePart.length() > 0 ) && |
| 96 | ( filePart.charAt(0) == '/' ) ? |
| 97 | filePart.substring(1) : |
| 98 | filePart ; |
| 99 | return getDictionaryReader( _environment , filePart ) ; |
| 100 | }else if( _protocol.equals( "cell" ) ){ |
| 101 | return getRemoteCellReader( _nucleus , url ) ; |
| 102 | }else |
| 103 | throw new IOException( "Protocol not supported : "+_protocol ) ; |
| 104 | } |
| 105 | public String getContentType(){ return "text/context" ; } |
| 106 | public String toString(){ return "DomainUrlConnection of : "+_protocol ; } |
| 107 | // |
| 108 | // helpers |
| 109 | // |
| 110 | private Reader getRemoteCellReader( CellNucleus nucleus , URL url ) |
| 111 | throws IOException { |
| 112 | |
| 113 | Object o = getRemoteData( nucleus , |
| 114 | url.getHost() , |
| 115 | url.getFile().substring(1) , |
| 116 | 4000 ) ; |
| 117 | |
| 118 | if( o instanceof Exception )throw new IOException( o.toString() ) ; |
| 119 | |
| 120 | return new StringReader( o.toString() ) ; |
| 121 | } |
| 122 | private Reader getRemoteContextReader( CellNucleus nucleus , URL url ) |
| 123 | throws IOException { |
| 124 | |
| 125 | Object o = getRemoteData( nucleus , |
| 126 | "System@"+url.getHost() , |
| 127 | "show context "+url.getFile().substring(1) , |
| 128 | 4000 ) ; |
| 129 | |
| 130 | if( o instanceof Exception )throw new IOException( o.toString() ) ; |
| 131 | |
| 132 | return new StringReader( o.toString() ) ; |
| 133 | } |
| 134 | private Object getRemoteData( CellNucleus nucleus , |
| 135 | String path , |
| 136 | String command , |
| 137 | long timeout ) |
| 138 | throws IOException { |
| 139 | |
| 140 | CellMessage answer = null ; |
| 141 | try{ |
| 142 | answer = nucleus.sendAndWait( |
| 143 | new CellMessage( new CellPath( path ) , |
| 144 | command ) , |
| 145 | timeout |
| 146 | ) ; |
| 147 | }catch( Exception e ){ |
| 148 | throw new IOException( "sendAndWait : "+e.toString() ) ; |
| 149 | } |
| 150 | if( answer == null ) |
| 151 | throw new IOException( "Request timed out" ) ; |
| 152 | |
| 153 | return answer.getMessageObject() ; |
| 154 | |
| 155 | } |
| 156 | private Reader getDictionaryReader( Map<String,Object> env , String name ) |
| 157 | throws IOException { |
| 158 | Object o ; |
| 159 | if( ( o = env.get( name ) ) == null ) |
| 160 | throw new IOException( "Not found : "+name ) ; |
| 161 | |
| 162 | return new StringReader( o.toString() ) ; |
| 163 | } |
| 164 | private Reader getDictionaryReaderx( Cell cell , String name ) |
| 165 | throws IOException { |
| 166 | |
| 167 | Class cellClass = cell.getClass() ; |
| 168 | _log.info("DomainUrlConnection : Cell Class is : "+cellClass ) ; |
| 169 | Class [] argsClasses = new Class[0] ; |
| 170 | try{ |
| 171 | Method method = cellClass.getDeclaredMethod( |
| 172 | "getEnvironmentDictionary" , |
| 173 | argsClasses ) ; |
| 174 | Object [] args = new Object[0] ; |
| 175 | |
| 176 | Dictionary dir = (Dictionary)method.invoke( cell , args ) ; |
| 177 | |
| 178 | Object o = dir.get( name ) ; |
| 179 | if( o == null ) |
| 180 | throw new IOException( "Not found : "+name ) ; |
| 181 | |
| 182 | return new StringReader( o.toString() ) ; |
| 183 | |
| 184 | }catch( Exception e ){ |
| 185 | throw new IOException( "Problem : "+e ) ; |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | } |
| 190 | public static void main(String args[]) throws Exception |
| 191 | { |
| 192 | new CellUrl(null); |
| 193 | System.out.println("checking the creation of context url"); |
| 194 | URL url1 = new URL("context://localhost:1111//sfs"); |
| 195 | System.out.println("checking the creation of cell url"); |
| 196 | URL url2 = new URL("cell://localhost:1111//sfs"); |
| 197 | System.out.println("checking the creation of env url"); |
| 198 | URL url3 = new URL("env://localhost:1111//sfs"); |
| 199 | System.out.println("done"); |
| 200 | } |
| 201 | } |