diff --git a/pom.xml b/pom.xml old mode 100755 new mode 100644 index a7cf76d9cd..9a021b67db --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,8 @@ 1.1.1.Final 2.7.0.Final 3.2.0 - 3.1.4.GA + 3.3.0.Final + 2.0.1.Final 1.2.0.Beta1 1.0.4.Final 1.0.0.Final @@ -297,6 +298,16 @@ jboss-logging ${jboss.logging.version} + + org.jboss.logging + jboss-logging-annotations + ${jboss.logging.tools.version} + + + org.jboss.logging + jboss-logging-processor + ${jboss.logging.tools.version} + log4j log4j diff --git a/services/pom.xml b/services/pom.xml index 3f7dba9618..64c4221995 100755 --- a/services/pom.xml +++ b/services/pom.xml @@ -57,6 +57,18 @@ org.jboss.logging jboss-logging + + org.jboss.logging + jboss-logging-annotations + provided + true + + + org.jboss.logging + jboss-logging-processor + provided + true + org.jboss.resteasy resteasy-jaxrs @@ -129,6 +141,9 @@ ${maven.compiler.source} ${maven.compiler.target} + + -AgeneratedTranslationFilesPath=${project.build.directory}/generated-translation-files + diff --git a/services/src/main/java/org/keycloak/services/ServicesLogger.java b/services/src/main/java/org/keycloak/services/ServicesLogger.java new file mode 100644 index 0000000000..e5c5029cf8 --- /dev/null +++ b/services/src/main/java/org/keycloak/services/ServicesLogger.java @@ -0,0 +1,89 @@ +/* + * Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package org.keycloak.services; + +import org.jboss.logging.BasicLogger; +import org.jboss.logging.Logger; +import org.jboss.logging.annotations.Cause; +import org.jboss.logging.annotations.LogMessage; +import org.jboss.logging.annotations.Message; +import org.jboss.logging.annotations.MessageLogger; + +import static org.jboss.logging.Logger.Level.ERROR; +import static org.jboss.logging.Logger.Level.INFO; +import static org.jboss.logging.Logger.Level.WARN; + + +/** + * Main logger for the Keycloak Services module. + * + * @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc. + */ +@MessageLogger(projectCode="KC-SERVICES", length=4) +public interface ServicesLogger extends BasicLogger { + + ServicesLogger ROOT_LOGGER = Logger.getMessageLogger(ServicesLogger.class, "org.keycloak.services"); + + @LogMessage(level = INFO) + @Message(id=1, value="Loading config from %s") + void loadingFrom(Object from); + + @LogMessage(level = ERROR) + @Message(id=2, value="Failed to migrate datamodel") + void migrationFailure(@Cause Throwable t); + + @LogMessage(level = INFO) + @Message(id=3, value="Not importing realm %s from %s. It already exists.") + void realmExists(String realmName, String from); + + @LogMessage(level = INFO) + @Message(id=4, value="Imported realm %s from %s.") + void importedRealm(String realmName, String from); + + @LogMessage(level = WARN) + @Message(id=5, value="Unable to import realm %s from %s.") + void unableToImportRealm(@Cause Throwable t, String realmName, String from); + + @LogMessage(level = INFO) + @Message(id=6, value="Importing users from '%s'") + void imprtingUsersFrom(Object from); + + @LogMessage(level = ERROR) + @Message(id=7, value="Failed to load 'keycloak-add-user.json'") + void failedToLoadUsers(@Cause Throwable t); + + @LogMessage(level = ERROR) + @Message(id=8, value="Failed to add user %s to realm %s: realm not found") + void addUserFailedRealmNotFound(String user, String realm); + + @LogMessage(level = INFO) + @Message(id=9, value="Added user '%s' to realm '%s'") + void addUserSuccess(String user, String realm); + + @LogMessage(level = ERROR) + @Message(id=10, value="Failed to add user '%s' to realm '%s': user with username exists") + void addUserFailedUserExists(String user, String realm); + + @LogMessage(level = ERROR) + @Message(id=11, value="Failed to add user '%s' to realm '%s'") + void addUserFailed(@Cause Throwable t, String user, String realm); + + @LogMessage(level = ERROR) + @Message(id=12, value="Failed to delete '%s'") + void failedToDeleteFile(String fileName); +} diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java old mode 100755 new mode 100644 index c0842eecfd..e619183ec1 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -3,7 +3,6 @@ package org.keycloak.services.resources; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import org.jboss.logging.Logger; import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.keycloak.Config; @@ -15,6 +14,7 @@ import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.services.DefaultKeycloakSessionFactory; +import org.keycloak.services.ServicesLogger; import org.keycloak.services.filters.KeycloakTransactionCommitter; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.RealmManager; @@ -44,7 +44,7 @@ import java.util.*; */ public class KeycloakApplication extends Application { - private static final Logger log = Logger.getLogger(KeycloakApplication.class); + private static final ServicesLogger log = ServicesLogger.ROOT_LOGGER; protected Set singletons = new HashSet(); protected Set> classes = new HashSet>(); @@ -135,7 +135,7 @@ public class KeycloakApplication extends Application { session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); - log.error("Failed to migrate datamodel", e); + log.migrationFailure(e); } finally { session.close(); } @@ -163,7 +163,7 @@ public class KeycloakApplication extends Application { if (configDir != null) { File f = new File(configDir + File.separator + "keycloak-server.json"); if (f.isFile()) { - log.info("Load config from " + f.getAbsolutePath()); + log.loadingFrom(f.getAbsolutePath()); node = new ObjectMapper().readTree(f); } } @@ -171,7 +171,7 @@ public class KeycloakApplication extends Application { if (node == null) { URL resource = Thread.currentThread().getContextClassLoader().getResource("META-INF/keycloak-server.json"); if (resource != null) { - log.info("Load config from " + resource); + log.loadingFrom(resource); node = new ObjectMapper().readTree(resource); } } @@ -247,22 +247,22 @@ public class KeycloakApplication extends Application { manager.setContextPath(getContextPath()); if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { - log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); + log.realmExists(rep.getRealm(), from); return; } if (manager.getRealmByName(rep.getRealm()) != null) { - log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); + log.realmExists(rep.getRealm(), from); return; } try { RealmModel realm = manager.importRealm(rep); session.getTransaction().commit(); - log.info("Imported realm " + realm.getName() + " from " + from); + log.importedRealm(realm.getName(), from); } catch (Throwable t) { session.getTransaction().rollback(); - log.warn("Unable to import realm " + rep.getRealm() + " from " + from + ". Cause: " + t.getMessage()); + log.unableToImportRealm(t, rep.getRealm(), from); } } finally { session.close(); @@ -274,14 +274,14 @@ public class KeycloakApplication extends Application { if (configDir != null) { File addUserFile = new File(configDir + File.separator + "keycloak-add-user.json"); if (addUserFile.isFile()) { - log.info("Importing users from '" + addUserFile + "'"); + log.imprtingUsersFrom(addUserFile); List realms; try { realms = JsonSerialization.readValue(new FileInputStream(addUserFile), new TypeReference>() { }); } catch (IOException e) { - log.errorv("Failed to load 'keycloak-add-user.json': {0}", e.getMessage()); + log.failedToLoadUsers(e); return; } @@ -293,7 +293,7 @@ public class KeycloakApplication extends Application { RealmModel realm = session.realms().getRealmByName(realmRep.getRealm()); if (realm == null) { - log.errorv("Failed to add user ''{0}'' to realm ''{1}'': realm not found", userRep.getUsername(), realmRep.getRealm()); + log.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm()); } else { UserModel user = session.users().addUser(realm, userRep.getUsername()); user.setEnabled(userRep.isEnabled()); @@ -302,12 +302,12 @@ public class KeycloakApplication extends Application { } session.getTransaction().commit(); - log.infov("Added user ''{0}'' to realm ''{1}''", userRep.getUsername(), realmRep.getRealm()); + log.addUserSuccess(userRep.getUsername(), realmRep.getRealm()); } catch (ModelDuplicateException e) { - log.errorv("Failed to add user ''{0}'' to realm ''{1}'': user with username exists", userRep.getUsername(), realmRep.getRealm()); + log.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm()); } catch (Throwable t) { session.getTransaction().rollback(); - log.errorv("Failed to add user ''{0}'' to realm ''{1}'': {2}", userRep.getUsername(), realmRep.getRealm(), t.getMessage()); + log.addUserFailed(t, userRep.getUsername(), realmRep.getRealm()); } finally { session.close(); } @@ -315,7 +315,7 @@ public class KeycloakApplication extends Application { } if (!addUserFile.delete()) { - log.errorv("Failed to delete '{0}'", addUserFile.getAbsolutePath()); + log.failedToDeleteFile(addUserFile.getAbsolutePath()); } } }