1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.transport;
13
14 import java.io.IOException;
15 import java.util.Collection;
16
17 import org.eclipse.jgit.annotations.Nullable;
18 import org.eclipse.jgit.lib.Config;
19 import org.eclipse.jgit.lib.Config.SectionParser;
20 import org.eclipse.jgit.lib.Repository;
21 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
22 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
23
24
25
26
27
28 public abstract class DaemonService {
29 private final String command;
30
31 private final SectionParser<ServiceConfig> configKey;
32
33 private boolean enabled;
34
35 private boolean overridable;
36
37 DaemonService(String cmdName, String cfgName) {
38 command = cmdName.startsWith("git-") ? cmdName : "git-" + cmdName;
39 configKey = cfg -> new ServiceConfig(DaemonService.this, cfg, cfgName);
40 overridable = true;
41 }
42
43 private static class ServiceConfig {
44 final boolean enabled;
45
46 ServiceConfig(final DaemonService service, final Config cfg,
47 final String name) {
48 enabled = cfg.getBoolean("daemon", name, service.isEnabled());
49 }
50 }
51
52
53
54
55
56
57 public boolean isEnabled() {
58 return enabled;
59 }
60
61
62
63
64
65
66
67
68 public void setEnabled(boolean on) {
69 enabled = on;
70 }
71
72
73
74
75
76
77
78
79 public boolean isOverridable() {
80 return overridable;
81 }
82
83
84
85
86
87
88
89
90
91
92 public void setOverridable(boolean on) {
93 overridable = on;
94 }
95
96
97
98
99
100
101 public String getCommandName() {
102 return command;
103 }
104
105
106
107
108
109
110
111
112 public boolean handles(String commandLine) {
113 return command.length() + 1 < commandLine.length()
114 && commandLine.charAt(command.length()) == ' '
115 && commandLine.startsWith(command);
116 }
117
118 void execute(DaemonClient client, String commandLine,
119 @Nullable Collection<String> extraParameters)
120 throws IOException, ServiceNotEnabledException,
121 ServiceNotAuthorizedException {
122 final String name = commandLine.substring(command.length() + 1);
123 try (Repository db = client.getDaemon().openRepository(client, name)) {
124 if (isEnabledFor(db)) {
125 execute(client, db, extraParameters);
126 }
127 } catch (ServiceMayNotContinueException e) {
128
129
130 PacketLineOut pktOut = new PacketLineOut(client.getOutputStream());
131 pktOut.writeString("ERR " + e.getMessage() + "\n");
132 }
133 }
134
135 private boolean isEnabledFor(Repository db) {
136 if (isOverridable())
137 return db.getConfig().get(configKey).enabled;
138 return isEnabled();
139 }
140
141 abstract void execute(DaemonClient client, Repository db,
142 @Nullable Collection<String> extraParameters)
143 throws IOException, ServiceNotEnabledException,
144 ServiceNotAuthorizedException;
145 }