View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.pgm;
45  
46  import java.io.File;
47  import java.net.InetSocketAddress;
48  import java.text.MessageFormat;
49  import java.util.ArrayList;
50  import java.util.List;
51  import java.util.concurrent.Executors;
52  
53  import org.eclipse.jgit.pgm.internal.CLIText;
54  import org.eclipse.jgit.storage.file.FileBasedConfig;
55  import org.eclipse.jgit.storage.file.WindowCacheConfig;
56  import org.eclipse.jgit.storage.pack.PackConfig;
57  import org.eclipse.jgit.transport.DaemonClient;
58  import org.eclipse.jgit.transport.DaemonService;
59  import org.eclipse.jgit.transport.resolver.FileResolver;
60  import org.eclipse.jgit.util.FS;
61  import org.kohsuke.args4j.Argument;
62  import org.kohsuke.args4j.Option;
63  
64  @Command(common = true, usage = "usage_exportRepositoriesOverGit")
65  class Daemon extends TextBuiltin {
66  	@Option(name = "--config-file", metaVar = "metaVar_configFile", usage = "usage_configFile")
67  	File configFile;
68  
69  	@Option(name = "--port", metaVar = "metaVar_port", usage = "usage_portNumberToListenOn")
70  	int port = org.eclipse.jgit.transport.Daemon.DEFAULT_PORT;
71  
72  	@Option(name = "--listen", metaVar = "metaVar_hostName", usage = "usage_hostnameOrIpToListenOn")
73  	String host;
74  
75  	@Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
76  	int timeout = -1;
77  
78  	@Option(name = "--enable", metaVar = "metaVar_service", usage = "usage_enableTheServiceInAllRepositories", multiValued = true)
79  	final List<String> enable = new ArrayList<String>();
80  
81  	@Option(name = "--disable", metaVar = "metaVar_service", usage = "usage_disableTheServiceInAllRepositories", multiValued = true)
82  	final List<String> disable = new ArrayList<String>();
83  
84  	@Option(name = "--allow-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename", multiValued = true)
85  	final List<String> canOverride = new ArrayList<String>();
86  
87  	@Option(name = "--forbid-override", metaVar = "metaVar_service", usage = "usage_configureTheServiceInDaemonServicename", multiValued = true)
88  	final List<String> forbidOverride = new ArrayList<String>();
89  
90  	@Option(name = "--export-all", usage = "usage_exportWithoutGitDaemonExportOk")
91  	boolean exportAll;
92  
93  	@Argument(required = true, metaVar = "metaVar_directory", usage = "usage_directoriesToExport")
94  	final List<File> directory = new ArrayList<File>();
95  
96  	@Override
97  	protected boolean requiresRepository() {
98  		return false;
99  	}
100 
101 	@Override
102 	protected void run() throws Exception {
103 		PackConfig packConfig = new PackConfig();
104 
105 		if (configFile != null) {
106 			if (!configFile.exists()) {
107 				throw die(MessageFormat.format(
108 						CLIText.get().configFileNotFound, //
109 						configFile.getAbsolutePath()));
110 			}
111 
112 			FileBasedConfig cfg = new FileBasedConfig(configFile, FS.DETECTED);
113 			cfg.load();
114 			new WindowCacheConfig().fromConfig(cfg).install();
115 			packConfig.fromConfig(cfg);
116 		}
117 
118 		int threads = packConfig.getThreads();
119 		if (threads <= 0)
120 			threads = Runtime.getRuntime().availableProcessors();
121 		if (1 < threads)
122 			packConfig.setExecutor(Executors.newFixedThreadPool(threads));
123 
124 		final FileResolver<DaemonClient> resolver = new FileResolver<DaemonClient>();
125 		for (final File f : directory) {
126 			outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
127 			resolver.exportDirectory(f);
128 		}
129 		resolver.setExportAll(exportAll);
130 
131 		final org.eclipse.jgit.transport.Daemon d;
132 		d = new org.eclipse.jgit.transport.Daemon(
133 				host != null ? new InetSocketAddress(host, port)
134 						: new InetSocketAddress(port));
135 		d.setPackConfig(packConfig);
136 		d.setRepositoryResolver(resolver);
137 		if (0 <= timeout)
138 			d.setTimeout(timeout);
139 
140 		for (final String n : enable)
141 			service(d, n).setEnabled(true);
142 		for (final String n : disable)
143 			service(d, n).setEnabled(false);
144 
145 		for (final String n : canOverride)
146 			service(d, n).setOverridable(true);
147 		for (final String n : forbidOverride)
148 			service(d, n).setOverridable(false);
149 
150 		d.start();
151 		outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
152 	}
153 
154 	private static DaemonService service(
155 			final org.eclipse.jgit.transport.Daemon d,
156 			final String n) {
157 		final DaemonService svc = d.getService(n);
158 		if (svc == null)
159 			throw die(MessageFormat.format(CLIText.get().serviceNotSupported, n));
160 		return svc;
161 	}
162 }