| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| examples.finger |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package examples; |
|
| 17 | ||
| 18 | import java.io.IOException; |
|
| 19 | import java.net.InetAddress; |
|
| 20 | import java.net.UnknownHostException; |
|
| 21 | import org.apache.commons.net.FingerClient; |
|
| 22 | ||
| 23 | /*** |
|
| 24 | * This is an example of how you would implement the finger command |
|
| 25 | * in Java using NetComponents. The Java version is much shorter. |
|
| 26 | * But keep in mind that the Unix finger command reads all sorts of |
|
| 27 | * local files to output local finger information. This program only |
|
| 28 | * queries the finger daemon. |
|
| 29 | * <p> |
|
| 30 | * The -l flag is used to request long output from the server. |
|
| 31 | * <p> |
|
| 32 | ***/ |
|
| 33 | 0 | public final class finger |
| 34 | { |
|
| 35 | ||
| 36 | public static final void main(String[] args) |
|
| 37 | { |
|
| 38 | 0 | boolean longOutput = false; |
| 39 | 0 | int arg = 0, index; |
| 40 | String handle, host; |
|
| 41 | FingerClient finger; |
|
| 42 | 0 | InetAddress address = null; |
| 43 | ||
| 44 | // Get flags. If an invalid flag is present, exit with usage message. |
|
| 45 | 0 | while (arg < args.length && args[arg].startsWith("-")) |
| 46 | { |
|
| 47 | 0 | if (args[arg].equals("-l")) |
| 48 | 0 | longOutput = true; |
| 49 | else |
|
| 50 | { |
|
| 51 | 0 | System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]"); |
| 52 | 0 | System.exit(1); |
| 53 | } |
|
| 54 | 0 | ++arg; |
| 55 | } |
|
| 56 | ||
| 57 | ||
| 58 | 0 | finger = new FingerClient(); |
| 59 | // We want to timeout if a response takes longer than 60 seconds |
|
| 60 | 0 | finger.setDefaultTimeout(60000); |
| 61 | ||
| 62 | 0 | if (arg >= args.length) |
| 63 | { |
|
| 64 | // Finger local host |
|
| 65 | ||
| 66 | try |
|
| 67 | { |
|
| 68 | 0 | address = InetAddress.getLocalHost(); |
| 69 | } |
|
| 70 | 0 | catch (UnknownHostException e) |
| 71 | { |
|
| 72 | 0 | System.err.println("Error unknown host: " + e.getMessage()); |
| 73 | 0 | System.exit(1); |
| 74 | 0 | } |
| 75 | ||
| 76 | try |
|
| 77 | { |
|
| 78 | 0 | finger.connect(address); |
| 79 | 0 | System.out.print(finger.query(longOutput)); |
| 80 | 0 | finger.disconnect(); |
| 81 | } |
|
| 82 | 0 | catch (IOException e) |
| 83 | { |
|
| 84 | 0 | System.err.println("Error I/O exception: " + e.getMessage()); |
| 85 | 0 | System.exit(1); |
| 86 | 0 | } |
| 87 | ||
| 88 | 0 | return ; |
| 89 | } |
|
| 90 | ||
| 91 | // Finger each argument |
|
| 92 | 0 | while (arg < args.length) |
| 93 | { |
|
| 94 | ||
| 95 | 0 | index = args[arg].lastIndexOf("@"); |
| 96 | ||
| 97 | 0 | if (index == -1) |
| 98 | { |
|
| 99 | 0 | handle = args[arg]; |
| 100 | try |
|
| 101 | { |
|
| 102 | 0 | address = InetAddress.getLocalHost(); |
| 103 | } |
|
| 104 | 0 | catch (UnknownHostException e) |
| 105 | { |
|
| 106 | 0 | System.err.println("Error unknown host: " + e.getMessage()); |
| 107 | 0 | System.exit(1); |
| 108 | 0 | } |
| 109 | } |
|
| 110 | else |
|
| 111 | { |
|
| 112 | 0 | handle = args[arg].substring(0, index); |
| 113 | 0 | host = args[arg].substring(index + 1); |
| 114 | ||
| 115 | try |
|
| 116 | { |
|
| 117 | 0 | address = InetAddress.getByName(host); |
| 118 | } |
|
| 119 | 0 | catch (UnknownHostException e) |
| 120 | { |
|
| 121 | 0 | System.err.println("Error unknown host: " + e.getMessage()); |
| 122 | 0 | System.exit(1); |
| 123 | 0 | } |
| 124 | } |
|
| 125 | ||
| 126 | 0 | System.out.println("[" + address.getHostName() + "]"); |
| 127 | ||
| 128 | try |
|
| 129 | { |
|
| 130 | 0 | finger.connect(address); |
| 131 | 0 | System.out.print(finger.query(longOutput, handle)); |
| 132 | 0 | finger.disconnect(); |
| 133 | } |
|
| 134 | 0 | catch (IOException e) |
| 135 | { |
|
| 136 | 0 | System.err.println("Error I/O exception: " + e.getMessage()); |
| 137 | 0 | System.exit(1); |
| 138 | 0 | } |
| 139 | ||
| 140 | 0 | ++arg; |
| 141 | 0 | if (arg != args.length) |
| 142 | 0 | System.out.print("\n"); |
| 143 | } |
|
| 144 | 0 | } |
| 145 | } |
|
| 146 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |