-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServiceModule.java
118 lines (104 loc) · 4.44 KB
/
ServiceModule.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
package com.mgu.photoalbum.config;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.mgu.photoalbum.adapter.fileio.PathAdapter;
import com.mgu.photoalbum.converter.AlbumReprConverter;
import com.mgu.photoalbum.converter.AlbumShortReprConverter;
import com.mgu.photoalbum.converter.GalleryReprConverter;
import com.mgu.photoalbum.converter.PhotoMetadataReprConverter;
import com.mgu.photoalbum.converter.PhotoShortReprConverter;
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.resource.LinkScheme;
import com.mgu.photoalbum.security.Authentication;
import com.mgu.photoalbum.security.Authorization;
import com.mgu.photoalbum.security.IdentityBasedAuth;
import com.mgu.photoalbum.service.AlbumCommandService;
import com.mgu.photoalbum.service.AlbumQueryService;
import com.mgu.photoalbum.service.AlbumService;
import com.mgu.photoalbum.service.PathScheme;
import com.mgu.photoalbum.service.PhotoCommandService;
import com.mgu.photoalbum.service.PhotoQueryService;
import com.mgu.photoalbum.service.PhotoService;
import com.mgu.photoalbum.service.scaler.ImageScaler;
import com.mgu.photoalbum.service.scaler.ScalrImageScaler;
import com.mgu.photoalbum.storage.AlbumRepository;
import com.mgu.photoalbum.storage.PhotoRepository;
import com.mgu.photoalbum.user.UserQueryService;
import com.mgu.photoalbum.user.UserRepository;
import com.mgu.photoalbum.user.UserService;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import javax.inject.Named;
public class ServiceModule extends AbstractModule {
@Provides
@Named("pathToArchive")
public String provideImageArchive(final ServiceConfig configuration) {
return configuration.getPhotoalbumConfig().getImageArchivePath();
}
@Provides
public CouchDbConnector provideConnection(final ServiceConfig config) {
final DatabaseConfig database = config.getDatabaseConfig();
try {
final HttpClient httpClient = new StdHttpClient.Builder()
.url(database.getUrl())
.connectionTimeout(10000)
.socketTimeout(10000)
.build();
final CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
return new StdCouchDbConnector(database.getName(), dbInstance);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
protected void configure() {
bind(IdGenerator.class);
try {
bind(PathScheme.class).toConstructor(PathScheme.class.getConstructor(String.class));
} catch (NoSuchMethodException e) {
addError(e);
}
bind(PathAdapter.class).toInstance(new PathAdapter());
configureAssetManagement();
configureUserManagement();
configureSecurity();
configureFacade();
}
private void configureAssetManagement() {
bind(ImageScaler.class).to(ScalrImageScaler.class);
bind(PhotoCommandService.class).to(PhotoService.class);
bind(PhotoQueryService.class).to(PhotoService.class);
bind(AlbumCommandService.class).to(AlbumService.class);
bind(AlbumQueryService.class).to(AlbumService.class);
try {
bind(AlbumRepository.class).toConstructor(AlbumRepository.class.getConstructor(CouchDbConnector.class));
bind(PhotoRepository.class).toConstructor(PhotoRepository.class.getConstructor(CouchDbConnector.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
private void configureUserManagement() {
bind(UserQueryService.class).to(UserService.class);
try {
bind(UserRepository.class).toConstructor(UserRepository.class.getConstructor(CouchDbConnector.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
private void configureSecurity() {
bind(Authentication.class).to(IdentityBasedAuth.class);
bind(Authorization.class).to(IdentityBasedAuth.class);
}
private void configureFacade() {
bind(LinkScheme.class);
bind(AlbumReprConverter.class);
bind(AlbumShortReprConverter.class);
bind(GalleryReprConverter.class);
bind(PhotoMetadataReprConverter.class);
bind(PhotoShortReprConverter.class);
}
}