~ubuntu-branches/ubuntu/raring/monodevelop/raring

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Transport/DaemonService.cs

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2011-06-29 06:56:25 UTC
  • mfrom: (1.8.1 upstream) (1.3.11 experimental)
  • Revision ID: james.westby@ubuntu.com-20110629065625-7xx19c4vb95j65pl
Tags: 2.5.92+dfsg-1ubuntu1
* Merge from Debian experimental:
 - Dropped patches & changes to debian/control for Moonlight
   + debian/patches/remove_support_for_moonlight.patch,
   + debian/patches/dont_add_moonlight_to_core_addins.patch,
 - Remaining patches:
   + debian/patches/no_appmenu:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using NGit;
 
45
using NGit.Transport;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit.Transport
 
49
{
 
50
        /// <summary>
 
51
        /// A service exposed by
 
52
        /// <see cref="Daemon">Daemon</see>
 
53
        /// over anonymous <code>git://</code>.
 
54
        /// </summary>
 
55
        public abstract class DaemonService
 
56
        {
 
57
                private readonly string command;
 
58
 
 
59
                private readonly Config.SectionParser<DaemonService.ServiceConfig> configKey;
 
60
 
 
61
                private bool enabled;
 
62
 
 
63
                private bool overridable;
 
64
 
 
65
                internal DaemonService(string cmdName, string cfgName)
 
66
                {
 
67
                        command = cmdName.StartsWith("git-") ? cmdName : "git-" + cmdName;
 
68
                        configKey = new _SectionParser_67(this, cfgName);
 
69
                        overridable = true;
 
70
                }
 
71
 
 
72
                private sealed class _SectionParser_67 : Config.SectionParser<DaemonService.ServiceConfig
 
73
                        >
 
74
                {
 
75
                        public _SectionParser_67(DaemonService _enclosing, string cfgName)
 
76
                        {
 
77
                                this._enclosing = _enclosing;
 
78
                                this.cfgName = cfgName;
 
79
                        }
 
80
 
 
81
                        public DaemonService.ServiceConfig Parse(Config cfg)
 
82
                        {
 
83
                                return new DaemonService.ServiceConfig(this._enclosing, cfg, cfgName);
 
84
                        }
 
85
 
 
86
                        private readonly DaemonService _enclosing;
 
87
 
 
88
                        private readonly string cfgName;
 
89
                }
 
90
 
 
91
                private class ServiceConfig
 
92
                {
 
93
                        internal readonly bool enabled;
 
94
 
 
95
                        internal ServiceConfig(DaemonService service, Config cfg, string name)
 
96
                        {
 
97
                                enabled = cfg.GetBoolean("daemon", name, service.IsEnabled());
 
98
                        }
 
99
                }
 
100
 
 
101
                /// <returns>is this service enabled for invocation?</returns>
 
102
                public virtual bool IsEnabled()
 
103
                {
 
104
                        return enabled;
 
105
                }
 
106
 
 
107
                /// <param name="on">true to allow this service to be used; false to deny it.</param>
 
108
                public virtual void SetEnabled(bool on)
 
109
                {
 
110
                        enabled = on;
 
111
                }
 
112
 
 
113
                /// <returns>can this service be configured in the repository config file?</returns>
 
114
                public virtual bool IsOverridable()
 
115
                {
 
116
                        return overridable;
 
117
                }
 
118
 
 
119
                /// <param name="on">
 
120
                /// true to permit repositories to override this service's enabled
 
121
                /// state with the <code>daemon.servicename</code> config setting.
 
122
                /// </param>
 
123
                public virtual void SetOverridable(bool on)
 
124
                {
 
125
                        overridable = on;
 
126
                }
 
127
 
 
128
                /// <returns>name of the command requested by clients.</returns>
 
129
                public virtual string GetCommandName()
 
130
                {
 
131
                        return command;
 
132
                }
 
133
 
 
134
                /// <summary>Determine if this service can handle the requested command.</summary>
 
135
                /// <remarks>Determine if this service can handle the requested command.</remarks>
 
136
                /// <param name="commandLine">input line from the client.</param>
 
137
                /// <returns>true if this command can accept the given command line.</returns>
 
138
                public virtual bool Handles(string commandLine)
 
139
                {
 
140
                        return command.Length + 1 < commandLine.Length && commandLine[command.Length] == 
 
141
                                ' ' && commandLine.StartsWith(command);
 
142
                }
 
143
 
 
144
                /// <exception cref="System.IO.IOException"></exception>
 
145
                /// <exception cref="NGit.Transport.Resolver.ServiceNotEnabledException"></exception>
 
146
                /// <exception cref="NGit.Transport.Resolver.ServiceNotAuthorizedException"></exception>
 
147
                internal virtual void Execute(DaemonClient client, string commandLine)
 
148
                {
 
149
                        string name = Sharpen.Runtime.Substring(commandLine, command.Length + 1);
 
150
                        Repository db = client.GetDaemon().OpenRepository(client, name);
 
151
                        if (db == null)
 
152
                        {
 
153
                                return;
 
154
                        }
 
155
                        try
 
156
                        {
 
157
                                if (IsEnabledFor(db))
 
158
                                {
 
159
                                        Execute(client, db);
 
160
                                }
 
161
                        }
 
162
                        finally
 
163
                        {
 
164
                                db.Close();
 
165
                        }
 
166
                }
 
167
 
 
168
                private bool IsEnabledFor(Repository db)
 
169
                {
 
170
                        if (IsOverridable())
 
171
                        {
 
172
                                return db.GetConfig().Get(configKey).enabled;
 
173
                        }
 
174
                        return IsEnabled();
 
175
                }
 
176
 
 
177
                /// <exception cref="System.IO.IOException"></exception>
 
178
                /// <exception cref="NGit.Transport.Resolver.ServiceNotEnabledException"></exception>
 
179
                /// <exception cref="NGit.Transport.Resolver.ServiceNotAuthorizedException"></exception>
 
180
                internal abstract void Execute(DaemonClient client, Repository db);
 
181
        }
 
182
}