commit | author | age
|
40aa84
|
1 |
package com.gitblit.git;
|
JM |
2 |
|
|
3 |
/*
|
|
4 |
* Copyright (C) 2008-2009, Google Inc.
|
|
5 |
* Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
|
|
6 |
* and other copyright owners as documented in the project's IP log.
|
|
7 |
*
|
|
8 |
* This program and the accompanying materials are made available
|
|
9 |
* under the terms of the Eclipse Distribution License v1.0 which
|
|
10 |
* accompanies this distribution, is reproduced below, and is
|
|
11 |
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
|
12 |
*
|
|
13 |
* All rights reserved.
|
|
14 |
*
|
|
15 |
* Redistribution and use in source and binary forms, with or
|
|
16 |
* without modification, are permitted provided that the following
|
|
17 |
* conditions are met:
|
|
18 |
*
|
|
19 |
* - Redistributions of source code must retain the above copyright
|
|
20 |
* notice, this list of conditions and the following disclaimer.
|
|
21 |
*
|
|
22 |
* - Redistributions in binary form must reproduce the above
|
|
23 |
* copyright notice, this list of conditions and the following
|
|
24 |
* disclaimer in the documentation and/or other materials provided
|
|
25 |
* with the distribution.
|
|
26 |
*
|
|
27 |
* - Neither the name of the Eclipse Foundation, Inc. nor the
|
|
28 |
* names of its contributors may be used to endorse or promote
|
|
29 |
* products derived from this software without specific prior
|
|
30 |
* written permission.
|
|
31 |
*
|
|
32 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
33 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
34 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
35 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
36 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
37 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
38 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
39 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
40 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
41 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
42 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
43 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
44 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
45 |
*/
|
|
46 |
|
|
47 |
import java.io.IOException;
|
|
48 |
|
|
49 |
import org.eclipse.jgit.lib.Config;
|
|
50 |
import org.eclipse.jgit.lib.Config.SectionParser;
|
|
51 |
import org.eclipse.jgit.lib.Repository;
|
|
52 |
import org.eclipse.jgit.transport.Daemon;
|
|
53 |
import org.eclipse.jgit.transport.PacketLineOut;
|
|
54 |
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
|
|
55 |
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
|
|
56 |
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
|
57 |
|
|
58 |
/** A service exposed by {@link Daemon} over anonymous <code>git://</code>. */
|
|
59 |
public abstract class GitDaemonService {
|
|
60 |
private final String command;
|
|
61 |
|
|
62 |
private final SectionParser<ServiceConfig> configKey;
|
|
63 |
|
|
64 |
private boolean enabled;
|
|
65 |
|
|
66 |
private boolean overridable;
|
|
67 |
|
|
68 |
GitDaemonService(final String cmdName, final String cfgName) {
|
|
69 |
command = cmdName.startsWith("git-") ? cmdName : "git-" + cmdName; //$NON-NLS-1$ //$NON-NLS-2$
|
|
70 |
configKey = new SectionParser<ServiceConfig>() {
|
|
71 |
public ServiceConfig parse(final Config cfg) {
|
|
72 |
return new ServiceConfig(GitDaemonService.this, cfg, cfgName);
|
|
73 |
}
|
|
74 |
};
|
|
75 |
overridable = true;
|
|
76 |
}
|
|
77 |
|
|
78 |
private static class ServiceConfig {
|
|
79 |
final boolean enabled;
|
|
80 |
|
|
81 |
ServiceConfig(final GitDaemonService service, final Config cfg,
|
|
82 |
final String name) {
|
|
83 |
enabled = cfg.getBoolean("daemon", name, service.isEnabled()); //$NON-NLS-1$
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/** @return is this service enabled for invocation? */
|
|
88 |
public boolean isEnabled() {
|
|
89 |
return enabled;
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* @param on
|
|
94 |
* true to allow this service to be used; false to deny it.
|
|
95 |
*/
|
|
96 |
public void setEnabled(final boolean on) {
|
|
97 |
enabled = on;
|
|
98 |
}
|
|
99 |
|
|
100 |
/** @return can this service be configured in the repository config file? */
|
|
101 |
public boolean isOverridable() {
|
|
102 |
return overridable;
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* @param on
|
|
107 |
* true to permit repositories to override this service's enabled
|
|
108 |
* state with the <code>daemon.servicename</code> config setting.
|
|
109 |
*/
|
|
110 |
public void setOverridable(final boolean on) {
|
|
111 |
overridable = on;
|
|
112 |
}
|
|
113 |
|
|
114 |
/** @return name of the command requested by clients. */
|
|
115 |
public String getCommandName() {
|
|
116 |
return command;
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Determine if this service can handle the requested command.
|
|
121 |
*
|
|
122 |
* @param commandLine
|
|
123 |
* input line from the client.
|
|
124 |
* @return true if this command can accept the given command line.
|
|
125 |
*/
|
|
126 |
public boolean handles(final String commandLine) {
|
|
127 |
return command.length() + 1 < commandLine.length()
|
|
128 |
&& commandLine.charAt(command.length()) == ' '
|
|
129 |
&& commandLine.startsWith(command);
|
|
130 |
}
|
|
131 |
|
|
132 |
void execute(final GitDaemonClient client, final String commandLine)
|
|
133 |
throws IOException, ServiceNotEnabledException,
|
|
134 |
ServiceNotAuthorizedException {
|
|
135 |
final String name = commandLine.substring(command.length() + 1);
|
|
136 |
Repository db;
|
|
137 |
try {
|
|
138 |
db = client.getDaemon().openRepository(client, name);
|
|
139 |
} catch (ServiceMayNotContinueException e) {
|
|
140 |
// An error when opening the repo means the client is expecting a ref
|
|
141 |
// advertisement, so use that style of error.
|
|
142 |
PacketLineOut pktOut = new PacketLineOut(client.getOutputStream());
|
|
143 |
pktOut.writeString("ERR " + e.getMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
144 |
db = null;
|
|
145 |
}
|
|
146 |
if (db == null)
|
|
147 |
return;
|
|
148 |
try {
|
|
149 |
if (isEnabledFor(db))
|
|
150 |
execute(client, db);
|
|
151 |
} finally {
|
|
152 |
db.close();
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
private boolean isEnabledFor(final Repository db) {
|
|
157 |
if (isOverridable())
|
|
158 |
return db.getConfig().get(configKey).enabled;
|
|
159 |
return isEnabled();
|
|
160 |
}
|
|
161 |
|
|
162 |
abstract void execute(GitDaemonClient client, Repository db)
|
|
163 |
throws IOException, ServiceNotEnabledException,
|
|
164 |
ServiceNotAuthorizedException;
|
|
165 |
}
|