In normal Java it is pretty easy to add your own custom loglevels.
But all the Java ways I know don't work in GWT because GWT has its own implementation of java.util.logging.Level.
Say I want an extra level between SEVERE (1000), and warning (900) --> ERROR (950).
How can I achieve this ?
I managed to sort this out for myself.
In case anyone is interested in this.
What you have to do is create a "super" directory. This on the same level as the "client" directory of your module.
In this directory you create a java.util.logging.Level class with the same content as the one found in the GWT source. But with the added level.
For example :
/*
* Copyright 2010 Google Inc.
*
* 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 java.util.logging;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.logging.impl.LevelImpl;
import com.google.gwt.logging.impl.LevelImplNull;
import java.io.Serializable;
/**
* An emulation of the java.util.logging.Level class. See
* <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/Level.html">
* The Java API doc for details</a>
*/
public class Level implements Serializable {
private static LevelImpl staticImpl = GWT.create(LevelImplNull.class);
public static Level ALL = new LevelAll();
public static Level CONFIG = new LevelConfig();
public static Level FINE = new LevelFine();
public static Level FINER = new LevelFiner();
public static Level FINEST = new LevelFinest();
public static Level INFO = new LevelInfo();
public static Level OFF = new LevelOff();
public static Level EXCEPTION = new LevelException();
public static Level WARNING = new LevelWarning();
public static Level ERROR = new LevelError();
public static Level SEVERE = new LevelSevere();
private static class LevelAll extends Level {
#Override public String getName() { return "ALL"; }
#Override public int intValue() { return Integer.MIN_VALUE; }
}
private static class LevelConfig extends Level {
#Override public String getName() { return "CONFIG"; }
#Override public int intValue() { return 700; }
}
private static class LevelFine extends Level {
#Override public String getName() { return "FINE"; }
#Override public int intValue() { return 500; }
}
private static class LevelFiner extends Level {
#Override public String getName() { return "FINER"; }
#Override public int intValue() { return 400; }
}
private static class LevelFinest extends Level {
#Override public String getName() { return "FINEST"; }
#Override public int intValue() { return 300; }
}
private static class LevelInfo extends Level {
#Override public String getName() { return "INFO"; }
#Override public int intValue() { return 800; }
}
private static class LevelOff extends Level {
#Override public String getName() { return "OFF"; }
#Override public int intValue() { return Integer.MAX_VALUE; }
}
private static class LevelException extends Level {
#Override public String getName() { return "EXCEPTION"; }
#Override public int intValue() { return 1000; }
}
private static class LevelSevere extends Level {
#Override public String getName() { return "SEVERE"; }
#Override public int intValue() { return 1000; }
}
private static class LevelError extends Level {
#Override public String getName() { return "ERROR"; }
#Override public int intValue() { return 950; }
}
private static class LevelWarning extends Level {
#Override public String getName() { return "WARNING"; }
#Override public int intValue() { return 900; }
}
public static Level parse(String name) {
return staticImpl.parse(name);
}
protected Level() { }
public String getName() {
return "DUMMY";
}
public int intValue() {
return -1;
}
#Override
public String toString() {
return getName();
}
/* Not Implemented */
// public boolean equals(Object ox) {}
// protected Level(String name, int value, String resourceBundleName) {}
// public String getLocalizedName() {}
// public String getResourceBundleName() {}
// public int hashCode() {}
}
Now you must mark this "super" directory as super source in your gwt.xml
<super-source path="super"/>
If you are using Eclipse you will have to exclude this super directory from the source path to avoid eclipse giving errors. And add it again as a seperate source path so that eclipse does check the file for errors.
Finally, if you do all this in a sub-module, you must ensure that when compiling your main module, this gets priority over the classes in rt.jar. This can be done by using an endorsed directory when compiling.
Related
So, I have values in getter setter variables when I click on form submit but now want to have those values in variables and check combination of them to run code from another java class
I have tried using parametrized constructor or may be having a common setter but that did not help.
package com.grt.dto;
import java.util.Set;
public class WDPayrollRecon {
public Set<String> dataType;
public String planCountry;
public String payPeriod;
public String currentPeriod;
public String lastPayPeriod;
Set<String> test;
public Set<String> getdataType() {
return dataType;
}
public void setdataType(Set<String> dataType) {
this.dataType = dataType;
System.out.println("this is dataType" +dataType);
test = dataType;
}
public String getPlanCountry() {
return planCountry;
}
public void setPlanCountry(String planCountry) {
this.planCountry = planCountry;
}
public String getPayPeriod() {
return payPeriod;
}
public void setPayPeriod(String payPeriod) {
this.payPeriod = payPeriod;
}
public String getCurrentPeriod() {
return currentPeriod;
}
public void setCurrentPeriod(String currentPeriod) {
this.currentPeriod = currentPeriod;
}
public String getlastPayPeriod() {
return lastPayPeriod;
}
public void setlastPayPeriod(String lastPayPeriod) {
this.lastPayPeriod = lastPayPeriod;
}
public WDPayrollRecon()
{
}
public WDPayrollRecon(Set<String> dataType,String planCountry,String payPeriod,String currentPeriod,String lastPayPeriod)
{
this.dataType = dataType;
this.planCountry = planCountry;
this.payPeriod = payPeriod;
this.currentPeriod = currentPeriod;
this.lastPayPeriod = lastPayPeriod;
if(dataType.contains("GTLI")& planCountry.equals("USA")){
System.out.println("This is test");
}
else{
System.out.println("This is not test");
}
}
}
I am trying to make a simple Spring Boot application generated with jHipster to get from a postgresql a list of articles from a postreSQL database and display it using a rest controller, but when i run it i get
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in org.adi.security.DomainUserDetailsService required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
But the thing is that that `DomainUserDetailsService is something generated by jhipster which stopped working after i added my classes. So i will write below my classes:
Article Entity:
package org.adi.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="articles")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="article_id")
private int articleId;
#Column(name="title")
private String title;
#Column(name="category")
private String category;
public int getArticleId() {
return articleId;
}
public void setArticleId(int articleId) {
this.articleId = articleId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
ArticleDAO (Repository):
#Transactional
#Repository
public class ArticleDAO implements IArticleDAO {
#PersistenceContext
private EntityManager entityManager;
#Override
public Article getArticleById(int articleId) {
return entityManager.find(Article.class, articleId);
}
#SuppressWarnings("unchecked")
#Override
public List<Article> getAllArticles() {
String hql = "FROM Article as atcl ORDER BY atcl.articleId DESC";
return (List<Article>) entityManager.createQuery(hql).getResultList();
}
#Override
public void createArticle(Article article) {
entityManager.persist(article);
}
#Override
public void updateArticle(Article article) {
Article artcl = getArticleById(article.getArticleId());
artcl.setTitle(article.getTitle());
artcl.setCategory(article.getCategory());
entityManager.flush();
}
#Override
public void deleteArticle(int articleId) {
entityManager.remove(getArticleById(articleId));
}
#Override
public boolean articleExists(String title, String category) {
String hql = "FROM Article as atcl WHERE atcl.title = ? and atcl.category = ?";
int count = entityManager.createQuery(hql).setParameter(1, title)
.setParameter(2, category).getResultList().size();
return count > 0 ? true : false;
}
}
ArticleService:
#Service
public class ArticleService implements IArticleService {
#Autowired
private IArticleDAO articleDAO;
#Override
public Article getArticleById(int articleId) {
Article obj = articleDAO.getArticleById(articleId);
return obj;
}
#Override
public List<Article> getAllArticles(){
return articleDAO.getAllArticles();
}
#Override
public synchronized boolean createArticle(Article article){
if (articleDAO.articleExists(article.getTitle(), article.getCategory())) {
return false;
} else {
articleDAO.createArticle(article);
return true;
}
}
#Override
public void updateArticle(Article article) {
articleDAO.updateArticle(article);
}
#Override
public void deleteArticle(int articleId) {
articleDAO.deleteArticle(articleId);
}
}
and finally my REST controller:
#Controller
#RequestMapping("user")
#CrossOrigin(origins = {"http://localhost:4200"})
public class ArticleController {
#Autowired
private IArticleService articleService;
#GetMapping("article")
public ResponseEntity<Article> getArticleById(#RequestParam("id") String id) {
Article article = articleService.getArticleById(Integer.parseInt(id));
return new ResponseEntity<Article>(article, HttpStatus.OK);
}
#GetMapping("all-articles")
public ResponseEntity<List<Article>> getAllArticles() {
List<Article> list = articleService.getAllArticles();
return new ResponseEntity<List<Article>>(list, HttpStatus.OK);
}
#PostMapping("article")
public ResponseEntity<Void> createArticle(#RequestBody Article article, UriComponentsBuilder builder) {
boolean flag = articleService.createArticle(article);
if (flag == false) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/article?id={id}").buildAndExpand(article.getArticleId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
#PutMapping("article")
public ResponseEntity<Article> updateArticle(#RequestBody Article article) {
articleService.updateArticle(article);
return new ResponseEntity<Article>(article, HttpStatus.OK);
}
#DeleteMapping("article")
public ResponseEntity<Void> deleteArticle(#RequestParam("id") String id) {
articleService.deleteArticle(Integer.parseInt(id));
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
}
Did you create a Persistence Unit?
[Reference]=> https://docs.oracle.com/cd/E19798-01/821-1841/bnbrj/index.html
Once you already have the persistence-unit's tag defined you are able to create your entity manager like this:
private final String PERSISTENCE_UNIT_NAME = "PUName";
private EntityManagerFactory eMFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager entityManager=eMFactory.createEntityManager();
I have InfraNameModel (Rest-type) to work with JSON
public interface IInfraNameBeanFactory extends AutoBeanFactory {
IInfraNameBeanFactory INSTANCE = GWT.create(IInfraNameBeanFactory.class);
AutoBean<InfraNameModel> infraName();
AutoBean<InfraNameListModel> results();
}
public interface InfraNameListModel {
List<InfraNameModel> getResults();
void setResults(List<InfraNameModel> results);
}
public class InfraNameListModelImpl implements InfraNameListModel {
private List<InfraNameModel> results;
#Override
public List<InfraNameModel> getResults() {
return results;
}
#Override
public void setResults(List<InfraNameModel> results) {
this.results = results;
}
}
public interface InfraNameModel {
String getInfraName();
void setInfraName(String infraName);
}
public class InfraNameModelImpl implements InfraNameModel {
private String infraName;
#Override
public String getInfraName() {
return infraName;
}
#Override
public void setInfraName(String infraName) {
this.infraName = infraName;
}
}
I wanted to make them into a separate JAR
To make it common for the client and the server
But now I have errors
[WARN] Class by.models.infraNameModel.InfraNameModel is used in Gin, but not available in GWT client code.
Is it real to pull such beans into a separate library?
GWT 2.5.0
A simple case using ListEditor failed below, what did i miss?
public class OneBean {
private String name;
public OneBean() {
}
public OneBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "OneBean [name=" + name + "]";
}
}
public class OneListEditor extends Composite implements
IsEditor<ListEditor<OneBean, OneEditor>> {
interface OneListUiBinder extends UiBinder<Widget, OneListEditor> {}
OneListUiBinder uiBinder = GWT.create(OneListUiBinder.class);
#UiField
VerticalPanel panel;
public OneListEditor() {
initWidget(uiBinder.createAndBindUi(this));
}
#Override
public ListEditor<OneBean, OneEditor> asEditor() {
return listEditor;
}
private ListEditor<OneBean, OneEditor> listEditor = ListEditor
.of(new EditorSource<OneEditor>() {
#Override
public OneEditor create(int index) {
OneEditor widget = new OneEditor();
panel.insert(widget, index);
return widget;
}
});
}
public class OneEditor extends Composite implements Editor<OneBean> {
interface OneUiBinder extends UiBinder<Widget, OneEditor> {}
OneUiBinder uiBinder = GWT.create(OneUiBinder.class);
#UiField
TextBox name;
public OneEditor() {
initWidget(uiBinder.createAndBindUi(this));
}
}
public class OneListEditorApp implements EntryPoint {
#Override
public void onModuleLoad() {
List<OneBean> beans = new ArrayList<OneBean>();
beans.add(new OneBean("1st bean"));
beans.add(new OneBean("2nd bean"));
OneListEditor oneListEditor = new OneListEditor();
oneListEditor.asEditor().setValue(beans); // exception thrown here!
RootPanel.get().add(oneListEditor);
}
}
java.lang.NullPointerException: null
at com.google.gwt.editor.client.adapters.ListEditorWrapper.attach(ListEditorWrapper.java:95)
at com.google.gwt.editor.client.adapters.ListEditor.setValue(ListEditor.java:164)
at OneListEditorApp.onModuleLoad ....
void attach() {
editors.addAll(editorSource.create(workingCopy.size(), 0));
for (int i = 0, j = workingCopy.size(); i < j; i++) {
chain.attach(workingCopy.get(i), editors.get(i)); // ListEditorWrapper NPE here!
}
}
#EDIT
According to the answer from #Thomas Broyer, NPE is gone after EditDriver being wired to OneListEditor below,
interface OneEditorDriver extends
SimpleBeanEditorDriver<OneBean, OneEditor> {}
OneEditorDriver driver = GWT.create(OneEditorDriver.class);
#Override
public ListEditor<OneBean, OneEditor> asEditor() {
listEditor.setEditorChain(new EditorChain<OneBean, OneEditor>() {
#Override
public OneBean getValue(OneEditor subEditor) {
return null;
}
#Override
public void detach(OneEditor subEditor) {
}
#Override
public void attach(OneBean object, OneEditor subEditor) {
driver.initialize(subEditor);
driver.edit(object);
}
});
return listEditor;
}
You're not using an EditorDriver, so the ListEditor is not initialized with an EditorChain, so chain is null, hence the NPE. Case made.
⇒ use an EditorDriver (or do not use a ListEditor)
How do I inject objects at runtime? For example, if I want to inject DerviedOne, DerivedTwo objects at runtime into the Test class in the following example, how do I do that? I found a few examples in Spring, but I'm not using Spring. This is a Dynamic Web Project with CDI using Java EE 6.
public abstract class Base
{
public Base(String initiator)
{
this.initiator = initiator;
}
public abstract void process();
public void baseProcess()
{
System.out.println("base process");
process();
}
public String getInitiator()
{
return initiator;
}
private String initiator;
}
public class BaseUtil
{
public long start()
{
return System.currentTimeMillis();
}
public long stop()
{
return System.currentTimeMillis();
}
}
public class DerivedOne extends Base
{
public DerivedOne(String initiator)
{
super(initiator);
}
#Override
public void process()
{
long start = baseUtil.start();
System.out.println(getInitiator() + " process");
long stop = baseUtil.stop();
System.out.println(stop - start);
}
#javax.inject.Inject
private BaseUtil baseUtil;
}
public class DerivedTwo extends Base
{
public DerivedTwo(String initiator)
{
super(initiator);
}
#Override
public void process()
{
long start = baseUtil.start();
System.out.println(getInitiator() + " process");
long stop = baseUtil.stop();
System.out.println(stop - start);
}
#javax.inject.Inject
private BaseUtil baseUtil;
}
#Startup
#Singleton
public class Test
{
#PostConstruct
public void init()
{
String initiator = "two";
Base base = null;
if("one".equals(initiator))
{
base = new DerivedOne("DerivedOne");
}
else if("two".equals(initiator))
{
base = new DerivedTwo("DerivedTwo");
}
base.baseProcess();
}
}
If you want to select one implementation based on runtime conditions You can use a producer method with qualifiers.
For testing CDI application I highly recommend Arquillian.
http://arquillian.org/