Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.args4j; 16 : 17 : import static com.google.gerrit.util.cli.Localizable.localizable; 18 : 19 : import com.google.gerrit.server.util.SocketUtil; 20 : import com.google.inject.Inject; 21 : import com.google.inject.assistedinject.Assisted; 22 : import java.net.SocketAddress; 23 : import org.kohsuke.args4j.CmdLineException; 24 : import org.kohsuke.args4j.CmdLineParser; 25 : import org.kohsuke.args4j.OptionDef; 26 : import org.kohsuke.args4j.spi.OptionHandler; 27 : import org.kohsuke.args4j.spi.Parameters; 28 : import org.kohsuke.args4j.spi.Setter; 29 : 30 : public class SocketAddressHandler extends OptionHandler<SocketAddress> { 31 : 32 : @Inject 33 : public SocketAddressHandler( 34 : @Assisted final CmdLineParser parser, 35 : @Assisted final OptionDef option, 36 : @Assisted final Setter<SocketAddress> setter) { 37 1 : super(parser, option, setter); 38 1 : } 39 : 40 : @Override 41 : public final int parseArguments(Parameters params) throws CmdLineException { 42 0 : final String token = params.getParameter(0); 43 : try { 44 0 : setter.addValue(SocketUtil.parse(token, 0)); 45 0 : } catch (IllegalArgumentException e) { 46 0 : throw new CmdLineException(owner, localizable(e.getMessage())); 47 0 : } 48 0 : return 1; 49 : } 50 : 51 : @Override 52 : public final String getDefaultMetaVariable() { 53 0 : return "HOST:PORT"; 54 : } 55 : }