net.java /* * Copyright (c) 2001-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import stec.sfc.Win32.Network; import stec.sfc.Win32.NetworkResourceEnumerator; import stec.sfc.Win32.NetworkResourceType; import stec.sfc.Win32.NetworkResourceDisplayType; import stec.sfc.Win32.NetworkResourceScope; import stec.sfc.Win32.NetworkResourceUsage; public class net { static void syntax() { System.out.println("usage: <javavm> net {<option> {<arguments>}}"); System.out.println(); System.out.println("option: -display {<remote_name>}"); System.out.println(" -help"); System.out.println(" -add <local_name> <remote_name> {<user_name> {<password>}}"); System.out.println(" -del <local_name>"); System.out.println(" -hostname"); System.out.println(" -user"); System.out.println(); } static void syntax(String message) { syntax(); if(message != null) { System.out.println(message); } System.out.println(); } private static void display(String server_name) throws Exception { if(!server_name.startsWith("\\\\")) { server_name = "\\\\" + server_name; } NetworkResourceEnumerator e = Network.getRemoteShares(server_name); try { while(e.next()) { System.out.println(e.getRemoteName()); } } finally { e.close(); } } private static void display() throws Exception { display(NetworkResourceScope.GLOBAL, NetworkResourceType.ANY, 0, NetworkResourceDisplayType.GENERIC, null, null, null, 0); } private static void display(int scope, int resource_type, int usage, int display_type, String provider, String local_name, String remote_name, int level) throws Exception { NetworkResourceEnumerator e = new NetworkResourceEnumerator(scope, resource_type, usage, display_type, provider, local_name, remote_name); try { while(e.next()) { if(e.getRemoteName() != null) { for(int i = 0; i < level; i++) { System.out.print("\t"); } System.out.println(e.getRemoteName()); if((e.getUsage() & NetworkResourceUsage.CONTAINER) == NetworkResourceUsage.CONTAINER) { display(e.getScope(), e.getResourceType(), e.getUsage(), e.getDisplayType(), e.getProvider(), e.getLocalName(), e.getRemoteName(), level + 1); } } } } finally { e.close(); } } public static void main(String[] args) throws Exception { int arg_count = args.length; if(arg_count == 0) { syntax(); return; } String arg = args[0].toLowerCase(); if(arg.equals("-display")) { if(arg_count < 2) { display(); } else { display(args[1]); } } else if(arg.equals("-help") || arg.equals("-h") || arg.equals("-?")) { syntax(); return; } else if(arg.equals("-add") || arg.equals("-a")) { if(arg_count < 3) { syntax("Invalid parameters."); } else if(arg_count == 3) { Network.addConnection(args[1], args[2], true); } else if(arg_count == 4) { Network.addConnection(args[1], args[2], args[3], null, true); } else if(arg_count == 5) { Network.addConnection(args[1], args[2], args[3], args[4], true); } else { syntax("Invalid parameters."); } } else if(arg.equals("-del") || arg.equals("-d")) { if(arg_count != 2) { syntax("local_name was not specified."); } Network.deleteConnection(args[1], true, false); } else if(arg.equals("-hostname")) { System.out.println(Network.getHostname()); } else if(arg.equals("-user")) { System.out.println(Network.getUserName()); } else { syntax("Invalid parameter: " + arg); } } }