BrightSide Workbench Full Report + Source Code
AbstractSessionContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.context;
20 
21 import java.io.Serializable;
22 import java.util.function.Supplier;
23 import org.turro.collections.Attributes;
24 
29 public abstract class AbstractSessionContext implements Serializable {
30 
31  /* Attributes */
32 
33  public Attributes getAttributes() {
34  return attributes;
35  }
36 
37  /* Constructor */
38 
39  private IConstructor constructor;
40 
42  return constructor;
43  }
44 
45  protected void setConstructor(IConstructor constructor) {
46  this.constructor = constructor;
47  }
48 
49  /* Factory */
50 
51  protected static <T extends AbstractSessionContext> T getInstance(IConstructor constructor, String type, Supplier<T> onCreate) {
52  String context = ElephantContext.getContextVariable(constructor) + type;
53  T sessionContext = (T) constructor.getSessionAttribute(context);
54  if(sessionContext == null) {
55  sessionContext = onCreate.get();
56  constructor.setSessionAttribute(context, sessionContext);
57  }
58  sessionContext.setConstructor(constructor);
59  return sessionContext;
60  }
61 
62  private final Attributes attributes;
63 
64  protected AbstractSessionContext() {
65  this.attributes = new Attributes();
66  }
67 
68 }
static< T extends AbstractSessionContext > T getInstance(IConstructor constructor, String type, Supplier< T > onCreate)
static String getContextVariable(IConstructor constructor)
void setSessionAttribute(String key, Object value)