/
Login.java
169 lines (149 loc) · 4.9 KB
/
Login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* ome.system.Login
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.system;
import java.util.Properties;
import ome.conditions.ApiUsageException;
/**
* Provides simplified handling of login properties when creating a
* {@link ome.system.ServiceFactory}. For more complicated uses,
* {@link java.util.Properties} can also be used. In which case, the constant
* {@link java.lang.String strings} provided in this class can be used as the
* keys to the {@link java.util.Properties properties instance} passed to
* {@link ome.system.ServiceFactory#ServiceFactory(Properties)}.
*
* @author Josh Moore <a
* href="mailto:josh.moore@gmx.de">josh.moore@gmx.de</a>
* @version 1.0
* @see ome.system.ServiceFactory
* @since 1.0
*/
public class Login {
/**
* Java property name for use in configuration of client login.
*/
public final static String OMERO_USER = "omero.user";
/**
* Java property name for use in configuration of client login.
*/
public final static String OMERO_GROUP = "omero.group";
/**
* Java property name for use in configuration of client login.
*/
public final static String OMERO_PASS = "omero.pass";
/**
* Java property name for use in configuration of client login.
*/
public final static String OMERO_EVENT = "omero.event";
/**
* {@link Login} constant which has username and password values set
* to null and other values set to their default. This will permit logging
* in as an anonymous user.
*/
public final static Login GUEST = new Login() {
@Override
public Properties asProperties() {
Properties p = super.asProperties();
p.setProperty(OMERO_USER, null);
p.setProperty(OMERO_PASS, null);
p.setProperty(OMERO_GROUP, null);
p.setProperty(OMERO_EVENT, null);
return p;
}
};
private String _user, _group, _pass, _event;
// Need at least user and password
private Login() {
}
/**
* standard constructor which leaves OMERO_GROUP and OMERO_EVENT null.
*
* @param user
* {@link ome.model.meta.Experimenter#getOmeName()}. Not null.
* @param password
* Cleartext password. Not null.
*/
public Login(String user, String password) {
if (user == null || password == null) {
throw new ApiUsageException("User and password arguments "
+ "to Login constructor cannot be null");
}
_user = user;
_pass = password;
}
/**
* extended constructor. As with {@link #Login(String, String)}, user and
* password may not be null.
*
* @param user
* {@link ome.model.meta.Experimenter#getOmeName()}. Not null.
* @param password
* Cleartext password. Not null.
* @param group
* Group name. May be null.
* @param event
* Enumeration value of the EventType. May be null.
*/
public Login(String user, String password, String group, String event) {
this(user, password);
_group = group;
_event = event;
}
// ~ Views
// =========================================================================
/**
* produces a copy of the internal fields as a {@link java.util.Properties}
* instance. Only those keys are present for which a field is non-null.
*
* @return Properties. Not null.
*/
public Properties asProperties() {
Properties p = new Properties();
p.setProperty(OMERO_USER, _user);
p.setProperty(OMERO_PASS, _pass);
if (_group != null) {
p.setProperty(OMERO_GROUP, _group);
}
if (_event != null) {
p.setProperty(OMERO_EVENT, _event);
}
return p;
}
/**
* simple getter for the user name passed into the constructor
*
* @return {@link ome.model.meta.Experimenter#getOmeName() user name}. Not
* null unless Login == {@link Login#GUEST}.
*/
public String getName() {
return _user;
}
/**
* simple getter for the password passed into the constructor
*
* @return password. Not null unless Login == {@link Login#GUEST}
*/
public String getPassword() {
return _pass;
}
/**
* simple getter for the group name passed into the constructor
*
* @return {@link ome.model.meta.ExperimenterGroup#getName() group name}.
* May be null.
*/
public String getGroup() {
return _group;
}
/**
* simple getter for the event type passed into the constructor
*
* @return {@link ome.model.enums.EventType event type}. May be null.
*/
public String getEvent() {
return _event;
}
}