001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.data.neo4j;
018
019import org.neo4j.ogm.config.AutoIndexMode;
020import org.neo4j.ogm.config.Configuration;
021import org.neo4j.ogm.config.Configuration.Builder;
022
023import org.springframework.beans.BeansException;
024import org.springframework.boot.context.properties.ConfigurationProperties;
025import org.springframework.context.ApplicationContext;
026import org.springframework.context.ApplicationContextAware;
027import org.springframework.util.ClassUtils;
028
029/**
030 * Configuration properties for Neo4j.
031 *
032 * @author Stephane Nicoll
033 * @author Michael Hunger
034 * @author Vince Bickers
035 * @author Aurélien Leboulanger
036 * @since 1.4.0
037 */
038@ConfigurationProperties(prefix = "spring.data.neo4j")
039public class Neo4jProperties implements ApplicationContextAware {
040
041        static final String EMBEDDED_DRIVER = "org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver";
042
043        static final String HTTP_DRIVER = "org.neo4j.ogm.drivers.http.driver.HttpDriver";
044
045        static final String DEFAULT_BOLT_URI = "bolt://localhost:7687";
046
047        static final String BOLT_DRIVER = "org.neo4j.ogm.drivers.bolt.driver.BoltDriver";
048
049        /**
050         * URI used by the driver. Auto-detected by default.
051         */
052        private String uri;
053
054        /**
055         * Login user of the server.
056         */
057        private String username;
058
059        /**
060         * Login password of the server.
061         */
062        private String password;
063
064        /**
065         * Auto index mode.
066         */
067        private AutoIndexMode autoIndex = AutoIndexMode.NONE;
068
069        /**
070         * Register OpenSessionInViewInterceptor. Binds a Neo4j Session to the thread for the
071         * entire processing of the request.",
072         */
073        private Boolean openInView;
074
075        private final Embedded embedded = new Embedded();
076
077        private ClassLoader classLoader = Neo4jProperties.class.getClassLoader();
078
079        public String getUri() {
080                return this.uri;
081        }
082
083        public void setUri(String uri) {
084                this.uri = uri;
085        }
086
087        public String getUsername() {
088                return this.username;
089        }
090
091        public void setUsername(String username) {
092                this.username = username;
093        }
094
095        public String getPassword() {
096                return this.password;
097        }
098
099        public void setPassword(String password) {
100                this.password = password;
101        }
102
103        public AutoIndexMode getAutoIndex() {
104                return this.autoIndex;
105        }
106
107        public void setAutoIndex(AutoIndexMode autoIndex) {
108                this.autoIndex = autoIndex;
109        }
110
111        public Boolean getOpenInView() {
112                return this.openInView;
113        }
114
115        public void setOpenInView(Boolean openInView) {
116                this.openInView = openInView;
117        }
118
119        public Embedded getEmbedded() {
120                return this.embedded;
121        }
122
123        @Override
124        public void setApplicationContext(ApplicationContext ctx) throws BeansException {
125                this.classLoader = ctx.getClassLoader();
126        }
127
128        /**
129         * Create a {@link Configuration} based on the state of this instance.
130         * @return a configuration
131         */
132        public Configuration createConfiguration() {
133                Builder builder = new Builder();
134                configure(builder);
135                return builder.build();
136        }
137
138        private void configure(Builder builder) {
139                if (this.uri != null) {
140                        builder.uri(this.uri);
141                }
142                else {
143                        configureUriWithDefaults(builder);
144                }
145                if (this.username != null && this.password != null) {
146                        builder.credentials(this.username, this.password);
147                }
148                builder.autoIndex(this.getAutoIndex().getName());
149        }
150
151        private void configureUriWithDefaults(Builder builder) {
152                if (!getEmbedded().isEnabled()
153                                || !ClassUtils.isPresent(EMBEDDED_DRIVER, this.classLoader)) {
154                        builder.uri(DEFAULT_BOLT_URI);
155                }
156        }
157
158        public static class Embedded {
159
160                /**
161                 * Whether to enable embedded mode if the embedded driver is available.
162                 */
163                private boolean enabled = true;
164
165                public boolean isEnabled() {
166                        return this.enabled;
167                }
168
169                public void setEnabled(boolean enabled) {
170                        this.enabled = enabled;
171                }
172
173        }
174
175}